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:
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.
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?
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