# \[Class learning hustle\] in python

**URL:** <https://forum.freecodecamp.org/t/class-learning-hustle-in-python/736526>\
**Category:** Curriculum Help\
**Tags:** python\
**Created:** [February 25, 2025, 9:38am UTC](https://forum.freecodecamp.org/t/class-learning-hustle-in-python/736526 "2025-02-25T09:38:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![06Douglas](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/06douglas/32/385708_2.png) [@06Douglas](https://forum.freecodecamp.org/u/06Douglas)\
**Post date:** [February 25, 2025, 9:38am UTC](https://forum.freecodecamp.org/t/class-learning-hustle-in-python/736526/1 "2025-02-25T09:38:37Z")

</div>

Hey there comrades hope your doing good coz am not, i hav just failed to add an attribute that can increment whenever an object is added, i tried to attach a unique part(\_num) use it as the attribute to increment it failed have a look:

 ![Screenshot_20250225-122007](https://global.discourse-cdn.com/freecodecamp/original/4X/3/f/e/3fed27cb14b01d98bdaddcac701233832fb7cc26.png)  
And the bare code he it comes…

---

<div class="post-metadata">

**Author:** ![06Douglas](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/06douglas/32/385708_2.png) [@06Douglas](https://forum.freecodecamp.org/u/06Douglas)\
**Post date:** [February 25, 2025, 9:39am UTC](https://forum.freecodecamp.org/t/class-learning-hustle-in-python/736526/2 "2025-02-25T09:39:44Z")

</div>

```py
class mypet:
    class_num =0
    
    def __init__ (self,name_num,breed_num):
        self.name_num=name_num
        self.breed_num=breed_num
        self.class_num+=1
    def my_method(self,comment):
        print(f"name : {self.name_num}\n breed-> {self.breed_num} \n behavior {comment} his humble..")
    def __str__ (self):
        return f"my object: {self.name_num},{self.breed_num}" 
    def my_other_method(self,comment2):
        print(f"for real: {self.name_num} \n is a {comment2} pet ")    
#call an instense of class (mypet) okay
pet=mypet("chalie","bulldog")
pet.my_method("impressively")
pet.my_other_method("Good")
pet=mypet("rocky","German shepherd")
pet.my_method("quit")
#we print the object with print (pet)
#the print(pet) calls the " __str__" method  
print(pet)
print(mypet.class_num)      
#cuty=mypet("chalie","bulldog")
#cuty.my_method("impresively")

```

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [February 25, 2025, 9:46am UTC](https://forum.freecodecamp.org/t/class-learning-hustle-in-python/736526/3 "2025-02-25T09:46:01Z")

</div>

can you describe more what you need to do for this challenge?

* * *

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

 ![Pre-formatted-text](https://global.discourse-cdn.com/freecodecamp/original/3X/1/b/1b18854f1059217f29370444396145cc3a0cd8ae.gif)

See [this post](https://forum.freecodecamp.org/t/forum-code-formatting/25574) to find the backtick on your keyboard.  
**Note:** Backticks (`) are not single quotes (').

---

<div class="post-metadata">

**Author:** ![sanity](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sanity/32/378897_2.png) [@sanity](https://forum.freecodecamp.org/u/sanity)\
**Post date:** [February 25, 2025, 1:37pm UTC](https://forum.freecodecamp.org/t/class-learning-hustle-in-python/736526/4 "2025-02-25T13:37:18Z")

</div>

What you are experiencing is subtlety between class and instance variables, while using `self.attribute` in classes.

Generally when `self.attribute` is used, first are checked instance attributes, if there’s no `attribute` among them, then class variables are checked (and if still there’s no `attribute`, then parent classes of the class are checked).

This is what happens when `attribute` is read in class. And the reason why `self.class_num` will give you number.

However that’s not what happens when something is assigned to `self.attribute`. In such case the `attribute` will be added to the instance variables, regardless of presence of `attribute` in class variables, etc.

`self.attribute += 1` is equivalent of `self.attribute = self.attribute + 1`. If there’s no `attribute` instance variable, but such class variable is present, what will happen is:

- `self.attribute` will be read from the class variables
- then result of `self.attribute + 1` will be saved in the instance variable
- From now on only instance variable will be accessed with `self.attribute`

What’s different way, in which class variable could be accessed?

---

<div class="post-metadata">

**Author:** ![06Douglas](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/06douglas/32/385708_2.png) [@06Douglas](https://forum.freecodecamp.org/u/06Douglas)\
**Post date:** [February 25, 2025, 2:40pm UTC](https://forum.freecodecamp.org/t/class-learning-hustle-in-python/736526/5 "2025-02-25T14:40:32Z")

</div>

Ahh! I see ehat you meant now its about the difference between class variable and instance var. When i used self. class\_num, python checked for it in the instance, snd when it didn’t it, it went to class but i tried to increment self. class\_num it actually created a new instance var, oh so its a matter of defining class\_num as class\_var directly in class using mypet. class\_num to increment… Hell yah your a life saver, Thanks a bunch sanity

---

<div class="post-metadata">

**Author:** ![06Douglas](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/06douglas/32/385708_2.png) [@06Douglas](https://forum.freecodecamp.org/u/06Douglas)\
**Post date:** [February 27, 2025, 3:00pm UTC](https://forum.freecodecamp.org/t/class-learning-hustle-in-python/736526/6 "2025-02-27T15:00:58Z")

</div>

Oh about your question Sanity, just use ClassName.variable instead of self.variable right? 🤔

---

<div class="post-metadata">

**Author:** ![sanity](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sanity/32/378897_2.png) [@sanity](https://forum.freecodecamp.org/u/sanity)\
**Post date:** [February 27, 2025, 3:05pm UTC](https://forum.freecodecamp.org/t/class-learning-hustle-in-python/736526/7 "2025-02-27T15:05:40Z")

</div>

Yep, that’s the proper way to access class variables.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [August 29, 2025, 3:06am UTC](https://forum.freecodecamp.org/t/class-learning-hustle-in-python/736526/8 "2025-08-29T03:06:17Z")

</div>

Closed on August 29, 2025, 3:06am UTC

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
