Trying to understand Python objects

James Stroud jstroud at mbi.ucla.edu
Wed Nov 22 01:33:00 CET 2006


walterbyrd wrote:
> Reading "Think Like a Computer Scientist" I am not sure I understand
> the way it describes the way objects work with Python.
> 
> 1) Can attributes can added just anywhere? I create an object called
> point, then I can add attributes any time, and at any place in the
> program?

Not all objects behave this way. For example, incidences of the object 
class:

py> j = object()
py> j.att = 2
------------------------------------------------------------
Traceback (most recent call last):
   File "<ipython console>", line 1, in <module>
<type 'exceptions.AttributeError'>: 'object' object has no attribute 'att'

Strangely enough, the exact rule is difficult to infer from the behavior 
of cPython:

py> def doit():
...   pass
...
py> doit.att = 2
py> doit.att
2
py> type(j)
<type 'object'>
py> type(doit)
<type 'function'>

Instances of your own classes and the classes you define yourself behave 
as you describe, though:

py> class C: pass
...
py> C.classatt = 2
py> C.classatt
2
py> c = C()
py> c.instat = 5
py> c.instat
5


> 2) Are classes typically created like this:
> 
> class Point:
>   pass

Its better for classes to descend from object, actually:

py> class Point(object): pass
...
py> type(Point)
<type 'type'>
py> type(C)
<type 'classobj'>

'type' type classes behave correctly with certain language features, 
such as properties. Last I checked 'classobj' classes do not. So 
new-style ('type' type) classes are preferred.

> Then attributes are added at some other time?

Yes. This goes for old (clasobj) and new style classes.

> 3) What is with the __underscores__ ??

They are hard to look at, but they are meant to signify implementation 
specific attributes of objects and not interface specific attributes. 
But, in my opinion, a lot of python code, especially "higher-level" code 
blurs the distinction, so it becomes a little underscore heavy.

> 4) Are parameters passed to an class definition?
> 
> class Whatever(params):
>    pass

No.

This is done under the implementation specific method called "__init__".

py> class D(object):
...   def __init__(self, param1, param2):
...     self.a = param1
...     self.b = param2
...
py> d = D(2,5)
py> d.a
2
py> d.b
5

> I sort-of understand the way objects work with PHP. With PHP, the
> classes are defined in one place - sort of like a function. To me, that
> makes more sense.

The difference (and I don't know PHP) is that many of the objects you 
create (classes and instances of those classes) are "mutable". Which 
means, of course that you can change them. I've found that good 
programming practice requires less of this mutability in general (i.e. 
adding attributes on the fly), but it is there if you need it. I think 
this flexibility is part the python philosophy, but I am not a python 
philosopher (nor was I meant to be).

James


-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/



More information about the Python-list mailing list