class implementation

Dave Angel davea at davea.name
Mon Sep 30 15:34:08 EDT 2013


On 30/9/2013 08:41, markotaht at gmail.com wrote:

> under variables, i mean, the int's and lists and strings and floats that the parent class uses. IF in parent class there is variable called location, then can i use the same variable in my sub class.

Python doesn't actually have variables, but the things it documents as
variables are local names within a method.  Those are not visible
outside of the method, regardless of whether you're in a class or a
subclass.

But perhaps you mean attributes.  There are both class attributes and
instance attributes, and the behavior is quite different.  Roughly
speaking a class attribute occurs only once per class, and all code can
read its value with either Class.my_attrib   or  instance.my_attrib.  It
can be written with Class.my_attrib.

On the other hand, instance attributes are usable by 
instance.my_attrib, regardless of whether the instance is a base class
or a child class.  Each instance of the class gets a separate copy of
such an attribute.  They are normally defined in the __init__() method.

If you don't happen to know the difference between a class an an
instance of that class, then all the above will look like gibberish, and
you need to do some studying first.

-- 
DaveA





More information about the Python-list mailing list