# Calling a class method in another class method?

**URL:** <https://discuss.python.org/t/calling-a-class-method-in-another-class-method/20871>\
**Category:** Python Help\
**Tags:** help\
**Created:** [November 8, 2022, 5:20pm UTC](https://discuss.python.org/t/calling-a-class-method-in-another-class-method/20871 "2022-11-08T17:20:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pepython](https://avatars.discourse-cdn.com/v4/letter/p/4bbf92/32.png) [@pepython](https://discuss.python.org/u/pepython)\
**Post date:** [November 8, 2022, 5:20pm UTC](https://discuss.python.org/t/calling-a-class-method-in-another-class-method/20871/1 "2022-11-08T17:20:32Z")

</div>

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()``

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [November 8, 2022, 10:04pm UTC](https://discuss.python.org/t/calling-a-class-method-in-another-class-method/20871/2 "2022-11-08T22:04:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vbrozik](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vbrozik/32/7423_2.png) [@vbrozik](https://discuss.python.org/u/vbrozik)\
**Post date:** [November 8, 2022, 11:20pm UTC](https://discuss.python.org/t/calling-a-class-method-in-another-class-method/20871/3 "2022-11-08T23:20:05Z")

</div>

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

> [@Python Discourse Quick Start](https://discuss.python.org/t/python-discourse-quick-start/116#how-do-i-post-code-snippets-error-messages-etc-4):
>
> Python Discourse Help [About](https://discuss.python.org/about)[FAQ](https://discuss.python.org/faq) Keyboard Shortcuts Found in top right “hamburger” menu or press ? How do I bookmark a message? If one clicks the … icon below a message, it is replaced by a flag white_flag and bookmark icons. (The latter looks like a hanging banner, but represents the exposed end of a fabric bookmark.) (Silly to have to do this, but that is another topic.) In any case, click your profile image and the left of the three icons underneath is the bookmark icon. Click that you …

* * *

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

> **[Method (computer programming) | Class methods](https://en.wikipedia.org/wiki/Method_(computer_programming)#Class_methods)**
>
> Class methods are methods that are called on a class rather than an instance. They are typically used as part of an object meta-model. I.e, for each class, defined an instance of the class object in the meta-model is created. Meta-model protocols allow classes to be created and deleted. In this sense, they provide the same functionality as constructors and destructors described above. But in some languages such as the Common Lisp Object System (CLOS) the meta-model allows the developer to dynami...

> **[Built-in Functions](https://docs.python.org/3/library/functions.html#classmethod)**
>
> The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order.,,,, Built-in Functions,,, A, abs(), aiter(), all(), a...

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

---

<div class="post-metadata">

**Author:** ![pepython](https://avatars.discourse-cdn.com/v4/letter/p/4bbf92/32.png) [@pepython](https://discuss.python.org/u/pepython)\
**Post date:** [November 9, 2022, 3:44pm UTC](https://discuss.python.org/t/calling-a-class-method-in-another-class-method/20871/4 "2022-11-09T15:44:01Z")

</div>

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…🙂 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

---

<div class="post-metadata">

**Author:** ![vbrozik](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vbrozik/32/7423_2.png) [@vbrozik](https://discuss.python.org/u/vbrozik)\
**Post date:** [November 9, 2022, 9:29pm UTC](https://discuss.python.org/t/calling-a-class-method-in-another-class-method/20871/5 "2022-11-09T21:29:08Z")

</div>

“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?
