class implementation

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Sep 30 20:19:30 EDT 2013


On Mon, 30 Sep 2013 05:41:16 -0700, markotaht 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.

Firstly, in Python circles we prefer to call them "attributes" rather 
than variables.

Since this is Python, it is trivially easy to test this for yourself. 
Start an interactive Python interpreter, and then in under a dozen lines 
you can test it:

py> class Test:
...     attr = "Hello World!"  # Shared class attribute.
...
py> class MyTest(Test):
...     pass
...
py> x = MyTest()
py> x.attr
'Hello World!'


Works perfectly! (It would be a funny programming language where it 
didn't, since this is one of the most fundamental parts of inheritance. A 
language that didn't do something equivalent to this couldn't really 
claim to be object-oriented.)



-- 
Steven



More information about the Python-list mailing list