[Class learning hustle] in python

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:


And the bare code he it comes…

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")

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.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

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?

1 Like

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

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

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

1 Like