[Tutor] class variables

Steven D'Aprano steve at pearwood.info
Sat Dec 21 18:40:48 CET 2013


On Sat, Dec 21, 2013 at 08:41:17AM -0500, eryksun wrote:
> On Sat, Dec 21, 2013 at 2:14 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> >
> > (Sometimes, people will call them "members", especially if they are used
> > to C#. The meaning here is member as in an arm or leg, as in
> > "dismember", not member in the sense of belonging to a group.)
> 
> A Python object isn't just a fixed-size block of data with members at
> fixed offsets. It stores its data dynamically in a dict.

Yes, but that's just an implementation detail. There's no fundamental 
difference between "attribute", "member" and "(instance/class) 
variable". Different languages give them different names according to 
whatever vagaries the language creator considers important. C# happens 
to use fixed-sized records, Python happens to (usually) use dicts.

Having said that, I do think it is useful to reserve the term "member" 
for the fixed-size block type, and "attribute" for the general term. At 
least when talking about Python.

[...]
> For example, the read-only __base__ attribute of a class uses the
> following descriptor:
> 
>     >>> vars(type)['__base__']
>     <member '__base__' of 'type' objects>

Oooh, nice! I always forget about vars(), and end up messing about with 
__dict__.


> > Suppose we ask Python for "polly.colour". Python looks at the instance
> > polly, and checks to see if it has an instance attribute called "polly".
[...]

> It first has to check Parrot and its base classes (in Method
> Resolution Order, i.e. Parrot.__mro__) for a data descriptor (e.g. a
> property) named "colour". An instance can't override a data
> descriptor.

I did say it was an over-simplified description. I didn't think it was 
helpful to start talking about descriptors to a beginner :-)

For what it's worth, descriptors are both absolutely fundamental to how 
Python operates, and an advanced feature that newbies don't need to 
understand immediately,


-- 
Steven


More information about the Tutor mailing list