Instance variables question

duncan smith duncan at invalid.invalid
Mon Apr 16 12:48:03 EDT 2018


On 16/04/18 17:03, Irv Kalb wrote:
> I have been writing OOP code for many years in other languages and for the past few years in Python.  I am writing new curriculum for a course on OOP in Python.  In order to see how others are explaining OOP concepts, I have been reading as many books and watching as many videos as I can.   I've been watching some videos created by Dr. Chuck Severance in a series called "Python For Everyone".  I think "Dr. Chuck" is an excellent teacher and I think his videos are outstanding.  
> 
> Today I watched this video:   https://www.youtube.com/watch?v=b2vc5uzUfoE <https://www.youtube.com/watch?v=b2vc5uzUfoE>  which is about 10 minutes long.  In that video he gives a very basic overview of OOP and classes.  He gives a demonstration using the following example:
> 
> class PartyAnimal():
>     x = 0
> 
>     def party(self):
>         self.x = self.x + 1
>         print('So far', self.x)
> 
> an = PartyAnimal()
> an.party()
> an.party()
> an.party()
> 
> # I added this line just to see what it would do
> print('Class variable', PartyAnimal.x)
> 
> 
> And the output is:
> 
> So far 1
> So far 2
> So far 3
> Class variable 0
> 
> But there is something there that seems odd.  My understanding is that the "x = 0" would be defining a class variable, that can be shared by all PartyAnimal objects.  But he explains that because x is defined between the class statement and the "party" method, that this defines an instance variable x.   That way, it can be used in the first line of the "party" method as self.x to increment itself.   
> 
> At the end of the video, he creates two objects from the same class, and each one gets its own self.x where each correctly starts at zero.  Again, I expected x to be a class variable (accessible through PartyAnimal.x).  
> 
> When I want to create an instance variable and to be used later in other methods, I do this:
> 
> class PartyAnimal():
>     def __init__(self):
>     	self.x = 0  
> 
>     def party(self):
>         self.x = self.x + 1
>         print('So far', self.x)
> 
> an = PartyAnimal()
> an.party()
> an.party()
> an.party()
> 
> That is, I would assign the instance variable in the __init__ method.  Both approaches give the same results.
> 
> I'm certainly not trying to argue with Dr. Chuck.  I am trying to understand his approach, but it's not clear to me why his code works.  Specifically, can anyone explain how his "x = 0" turns x into an instance variable - while also allowing the syntax for a class variable PartyAnimal.x to be used?
> 
> Thanks,
> 
> Irv
> 

My understanding of this:

x is a class variable.

Initially an instance has no instance variable self.x. So on the first
call to self.party the name 'x' is looked up in the class. This value is
incremented and the result is assigned to self.x. This is where the
instance variable x is created and set. On subsequent calls to
self.party there exists an instance variable x, so the name 'x' is not
looked up in the class.

Duncan


More information about the Python-list mailing list