Calling a class method in another class method?

Hello everyone and sorry if I’m not in the right section it’s my first time posting here.
I would like to know how you can use a class method in another method. Below you will have an example with the automaticshuffle method where I want to call the shuffle method under a certain condition but it doeszn’t work. How am I supposed to do that?

``import random
class Deck:
def init(self):
cards=
suit=(“Hearts”, “Diamonds”, “Clubs”, “Spades”)
value=(“A”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“10”,“J”,“Q”,“K”)
for i in suit:
for j in value:
cards.append((i,j))
random.shuffle(cards)
self.cards=cards
self.suit=suit
self.value=value

def deal(self):
dealt=self.cards.pop()
return dealt

def shuffle(self):
if len(self.cards)<52:
cards=
suit=(“Hearts”, “Diamonds”, “Clubs”, “Spades”)
value=(“A”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“10”,“J”,“Q”,“K”)
for i in suit:
for j in value:
cards.append((i,j))
random.shuffle(cards)
self.cards=cards

def automaticshuffle(self):
if len(self.cards)==0:
self.shuffle()``

You need to use 3 back ticks to quote you code and seem to have only used 2.

1 Like

And the three backticks have to be on a dedicated line (without other text).
I have added short instructions to the Quick Start post:


To your question: “class method” is a terminus technicus:

https://docs.python.org/3/library/functions.html#classmethod

I did not notice a class method in your code. You probably meant an instance method?

I probably haven’t use the right term but I thought “method” was the name you give to a function (something that is defined with def…:slight_smile: that belongs to a class. Here I want to call shuffle inside another function which is auomaticshuffle. I read the document you send but I’m not refferring to that even if my name is probably incorrect

“method” is a correct term but “class method” is a special kind of method.

  • method” or “instance method” is bound to an object instance — It looks like this is what you are interested in.
  • class method” is bound to the class — You do not need to know this at the beginning but do not use this term for something different.

Anyway if you want help regarding your code please use the triple backticks so we can see the code properly.

Also: What exactly does mean “it doeszn’t work”? What behaviour do you expect and what do you get instead?