Trying to understand Python objects

W

walterbyrd

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?

2) Are classes typically created like this:

class Point:
pass

Then attributes are added at some other time?

3) What is with the __underscores__ ??

4) Are parameters passed to an class definition?

class Whatever(params):
pass

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.
 
J

John Henry

classes are not functions. They are a "thing".

A simple example:

class pet(object):

def __init__(self):
self.myLegs=self._SetNumberOfLegs()

def _SetNumberOfLegs(self):
return 0

def HowManyLegsDoYouHave(self):
print self.myLegs

class cat(pet):

def _SetNumberOfLegs(self):
return 4

class rabbit(pet):

def _SetNumberOfLegs(self):
return 2

class fish(pet):
pass

myPets=[cat(), fish(), rabbit()]

for pet in myPets:
pet.HowManyLegsDoYouHave()


In the above example, both cat and rabbit has legs and fish? Not
normally. :=)
 
G

Gabriel Genellina

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?

For "normal" python objects, yes, you can add/delete any attribute,
anytime, anywhere. There are hooks where you can actively deny
attribute creation, or "freeze" the attribute list, but these are
considered advanced usage.
2) Are classes typically created like this:

class Point:
pass

Then attributes are added at some other time?

Not "typically"! Although you *can* do that, doesn't mean that you
*must* do things that way.
A 2D point might say:

class Point2D(object):
def __init__(self, x, y):
self.x = x
self.y = y

def distance(self, other):
return math.hypot(other.x-self.x, other.y-self.y)

p0 = Point2D(3, 2)
p1 = Point2D(0, 6)
p0.distance(p1) # gives 5
3) What is with the __underscores__ ??

Names that begin and end in __ are used by Python itself and have
some "magic" attached sometimes. The most common is __init__ (used to
initialize a new instance). __str__ by example, is used to get an
object's textual representation suitable for printing. If you executeusing the example above, you'll get something like <Point2D instance
at 0x12345678>. Add the following to the Point2D class and you get a nice pair:

def __str__(self):
return '(%f, %f)' % (self.x, self.y)
4) Are parameters passed to an class definition?

class Whatever(params):
pass

If you see something like that, "params" are not parameters, but base
classes. The Whatever class inherits all attributes and methods known
to its bases, and may provide its own.
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.

Usually in Python you also define a class in only one place - you
*can* modify it later, and that's a powerful feature, but certainly
you don't have to!


--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
 
J

James Stroud

walterbyrd said:
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/
 
G

George Sakkis

walterbyrd said:
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?

For user-defined (i.e. not builtin) classes, yes. There are ways to
prevent this (or at least it make it harder), but they're usually not
worth it unless you have pretty good reasons to do it. And just in case
you're wondering, "that's how php/c++/java/whatever does it" doesn't
count as a pretty good reason.
2) Are classes typically created like this:

class Point:
pass

Then attributes are added at some other time?

Yes, you can do it. No, it is not typical. The typical is to initialize
all class attributes in the class body, and all instance attributes in
__init__:

class Point(object): # prefer new-style classes in new code
num_instances = 0 # a class attribute

def __init__(self, x, y):
self.x, self.y = x,y # instance attributes

There are cases where this is not possible or desirable; Python doesn't
get in your way in these cases, but they are certainly not typical.
3) What is with the __underscores__ ??

They're special attributes, usually methods that perform "operator
overloading". If the term doesn't mean anything to you, think of it as
a method that is called implicitly by a specific operator. E.g. instead
of having a normal method add() and then call it as a.add(b), you can
define the special method __add__ and have it called when you write a +
b. There are several details I left out but that's the gist of it.
4) Are parameters passed to an class definition?

class Whatever(params):
pass

No, these are not parameters, they are the base (aka "super") clasess
of Whatever.
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.

It makes sense in Python too, see point (2). The difference is that for
the few cases that it either doesn't make sense or it's less convenient
or desirable, Python doesn't get in your way.

HTH,
George
 
B

Ben Finney

walterbyrd said:
Reading "Think Like a Computer Scientist" I am not sure I understand
the way it describes the way objects work with Python.

Congratulations for taking the effort to figure it out, and for
thinking about the questions you want answered.
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?

Generally, yes. It's up to the programmer to do so in a way that's
readable and makes sense to later readers. There are conventions, of
course, and you should ask about anything specific that confuses you.

One convention that you seem to be unaware of is that assigning
attributes outside of the class's methods is unusual. This is only a
convention, though, and not enforced by the language; on the uncommon
occasions you need to add attributes to an instance outside that
class's methods, you're not prevented from doing it.
2) Are classes typically created like this:

class Point:
pass

Typically, classes are created as a subclass of another class. The
top-level basic type in Python is 'object', so if your class doesn't
make sense deriving from anything else, derive from 'object'.

class Point(object):
pass

Defining a class with *no* superclass is not recommended. If you don't
yet understand the difference between the above style (called a
"new-style" class) and the style you presented, you should always
derive from a superclass ('object' or something more specific) until
you encounter a situation where that causes a problem.
Then attributes are added at some other time?

No. Typically, the desired attributes are added either to the class
when it is defined, resulting in an attribute value that is shared by
all instances of that class::

class Point(object):
spam = 4

Or, the attributes are added to a specific instance (often in the
initialisation method) so that each instance has a separate attribute
with the same name::

class Point(object):
spam = 4
def __init__(self):
eggs = 2
3) What is with the __underscores__ ??

Another convention. Names of the form '__foo__' are a signal to the
reader that this name will be treated specially; often the internals
of Python syntax or some builtin type will look for those names when
performing behind-the-scenes magic.

For instance, the '__init__' attribute of a class is expected to be a
function, and that function is called by Python's instance creation
steps immediately after the creation of an instance. Thus, it is used
to "initialise" the newly-created instance of the class, doing things
specific to that instance.
4) Are parameters passed to an class definition?

class Whatever(params):
pass

The parameters of the class definition are the super-classes from
which this class should inherit its attributes. They don't correspond
with the parameters to the class constructor.

When you create a new instance of a class, its constructor is called
to create the new instance, and then the initialiser is called, to
perform post-creation steps on the new instance. This all occurs in
one step, as far as the code that called the constructor is concerned.

Unlike in many other languages, you will rarely need to define a
constructor ('__new__') for your Python classes. It's far more common
to let the default constructor create the object, and have any
initialisation steps on the newly-created instance performed in the
class '__init__' method.

The __init__ method gets called by Python's internals (hence the
"special" name with underscores) immediately after the creation of the
instance. It is called with parameters consisting of the newly-created
instance object, followed by all the parameters that were passed to
the constructor.

origin = Point(0, 0)

This will call the constructor of the Point class, which will create a
new instance; then, the __init__ method of the Point class will be
called with the parameters (<new Point instance>, 0, 0). This means
the function has access to the specific instance, and can modify it
accordingly.

The convention is to use 'self' as the name to refer to the first
parameter to instance methods, so that the instance object is referred
to by the parameter name 'self' in that method.

class Point(object):
spam = 4
def __init__(self, x_coord, y_coord):
if x_coord < 0:
x_coord = 0
if y_coord < 0:
y_coord = 0
self.x = x_coord
self.y = y_coord

This initialisation method does a bit of value fudging, then assigns
values to attributes of the instance, named 'self' in the context of
the method.

After the constructor and the __init__ method have finished, the
program continues at the point where the constructor was called. Thus,
the attributes assigned to the instance are immediately available::

origin = Point(0, 0)
print origin.x
 
G

George Sakkis

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

(snipped)


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

(snipped)

I think this flexibility is part the python philosophy, but I am not a python
philosopher (nor was I meant to be).

Neither a python tutor, apparently, but strong candidate for the
monthly "most confusing-to-a-newbie reply" award.

George
 
B

Ben Finney

Ben Finney said:
Or, the attributes are added to a specific instance (often in the
initialisation method) so that each instance has a separate attribute
with the same name::

The example here should have been::

class Point(object):
spam = 4
def __init__(self):
self.eggs = 2
 
R

Robert Kern

Ben said:
Or, the attributes are added to a specific instance (often in the
initialisation method) so that each instance has a separate attribute
with the same name::

class Point(object):
spam = 4
def __init__(self):
eggs = 2

There's a typo there. For the sake of not confusing the OP, this is correct:

class Point(object):
spam = 4
def __init__(self):
self.eggs = 2


--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
F

Fredrik Lundh

walterbyrd said:
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?

in general, yes, but that should be done sparingly.
2) Are classes typically created like this:

class Point:
pass

Then attributes are added at some other time?

no, classes with attributes are typically created like this:

class Point:
def __init__(self, x, y):
self.x = x
self.y = y

p = Point(1, 2)

i.e. attributes are created in the initialization function.
3) What is with the __underscores__ ??

they're reserved for use by the interpreter, usually for calling special
methods in you class:

http://effbot.org/pyref/reserved-identifier-classes
http://effbot.org/pyref/special-method-names
4) Are parameters passed to an class definition?

class Whatever(params):
pass

no, that's the syntax used for inheritance. arguments passed to the
class constructor are passed on to the initialization function (see the
example above).

for more on this, see the tutorial. Jay Parlar's gentle introduction
from the community tutorial might be helpful:

http://effbot.org/pytut/node11-baseline.htm

</F>
 
B

Bruno Desthuilliers

walterbyrd said:
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?

Apart from a few special cases (mainly builtin types or new-style
classes using slots IIRC), yes.

Now wether this is a good idea is another question. As far as I'm
concerned, I do use this feature, but only for special cases, and almost
only in metaclasses or other special 2-stages init.
2) Are classes typically created like this:

class Point:
pass

Then attributes are added at some other time?

Nope. The canonical idiom is to use the initializer:

class Point(object):
def __init__(self, attr1, attr2):
self.attr1 = attr1
self.attr2 = attr2
self.attr3 = 42

3) What is with the __underscores__ ??

"magic" methods or attributes. The initializer ('__init__') is one of
them, which is called at instance creation time. Most of the magic
__methods__ are used to implement or override operators. You'll find the
relevant documentation around here:
http://docs.python.org/ref/specialnames.html

4) Are parameters passed to an class definition?

class Whatever(params):
pass

A class statement is not a function definition statement. Here, you ask
for a class Whatever inheriting from class params. Cf the above point
about the __init__ method for passing args at instanciation.
I sort-of understand the way objects work with PHP. With PHP, the
classes are defined in one place - sort of like a function.

FWIW, Python's classes actually are callable objects (just like
functions are callable objects). To instanciate a class, you just call
the class, no 'new' keyword needed.
To me, that
makes more sense.

<?php

class Obj {
// pass
}

$obj = new Obj();
$obj->name = "toto";
echo $obj->name;

?>

Just like Python (and Javascript FWIW), PHP objects are mostly hashtable
in disguise. But - having some experience with both PHP and Python (and
some other OOPLs), I can tell you that Python's object model is far
superior to PHP's one.

The only "gotcha" here wrt/ most other object models is that attributes
defined in the class statement body (ie, outside methods) are attached
to the class object itself (and then shared by all instances of the
class), not to instances themselves. Instance attributes initialisation
is usually done in the __init__(self, ...) method.

HTH
 
W

walterbyrd

Thanks everybody. I will sort all of this out, but right now my head is
spinning.

Is there some book, or other reference, that explains of this? I was
thinking about "Python for Dummies." The "Think like a Computer
Scientist" book, and "Dive into Python" book don't seem to explain
Python's object model clearly enough for me.
 
B

Ben Finney

walterbyrd said:
Is there some book, or other reference, that explains of this? I was
thinking about "Python for Dummies." The "Think like a Computer
Scientist" book, and "Dive into Python" book don't seem to explain
Python's object model clearly enough for me.

The canonical introduction is the Python tutorial::

<URL:http://docs.python.org/tut/>

To get benefit from this, you should *work through* this document, not
merely read it. Do each exercise and don't go on until you believe you
understand what it's doing.
 
A

Aahz

Typically, classes are created as a subclass of another class. The
top-level basic type in Python is 'object', so if your class doesn't
make sense deriving from anything else, derive from 'object'.

class Point(object):
pass

Defining a class with *no* superclass is not recommended. If you don't
yet understand the difference between the above style (called a
"new-style" class) and the style you presented, you should always
derive from a superclass ('object' or something more specific) until
you encounter a situation where that causes a problem.

Side note: I disagree with the above advice, but it's Thanksgiving and I
don't have enough room on the margin for the proof. I think classic
classes are just fine.
--
Aahz ([email protected]) <*> http://www.pythoncraft.com/

"In many ways, it's a dull language, borrowing solid old concepts from
many other languages & styles: boring syntax, unsurprising semantics,
few automatic coercions, etc etc. But that's one of the things I like
about it." --Tim Peters on Python, 16 Sep 1993
 
A

Aahz

Is there some book, or other reference, that explains of this? I was
thinking about "Python for Dummies." The "Think like a Computer
Scientist" book, and "Dive into Python" book don't seem to explain
Python's object model clearly enough for me.

Speaking as the co-author of _Python for Dummies_, we certainly tried to
make much of this clear, but I haven't seen any reviews yet (partly our
fault for not getting out review copies).
--
Aahz ([email protected]) <*> http://www.pythoncraft.com/

"In many ways, it's a dull language, borrowing solid old concepts from
many other languages & styles: boring syntax, unsurprising semantics,
few automatic coercions, etc etc. But that's one of the things I like
about it." --Tim Peters on Python, 16 Sep 1993
 
B

Bruno Desthuilliers

Aahz a écrit :
Side note: I disagree with the above advice, but it's Thanksgiving and I
don't have enough room on the margin for the proof. I think classic
classes are just fine.

Don't see it as a religious point please, but I fail to understand why
you seem so in love with old-style classes ? new-style classes are the
"official" Python object model since 2.2 (which is a few years ago now),
and the last mandatory use of them (exceptions...) disappeared with the
2.5. AFAIK, everything you do with old-style classes can be done with
new-style ones. FWIW, old-style classes support is now only for backward
compat. So *why* insisting on using them ?

wondering...
 
G

George Sakkis

Bruno said:
AFAIK, everything you do with old-style classes can be done with new-style ones.

The only thing I occasionally (or rather rarely) miss about old-style
classes is instance-specific special methods:
.... def __init__(self,x):
.... self.__getitem__ = lambda i: i*x
....
.... def __init__(self,x):
.... self.__getitem__ = lambda i: i*x
....
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unindexable object

Of course this example can be rewritten to work for new style classes;
a trickier would be to bind the instance attribute conditionally;
here's a silly example:
.... def __init__(self,x):
.... if random.random() > 0.5:
.... self.__getitem__ = lambda i: i*x

I'm not sure if this is a conscious choice or a technical limitation of
how new-style classes work internally, but I've had a use for it at
least once.

George
 
F

Fredrik Lundh

Bruno said:
Don't see it as a religious point please, but I fail to understand why
you seem so in love with old-style classes ? new-style classes are the
"official" Python object model since 2.2 (which is a few years ago now),
and the last mandatory use of them (exceptions...) disappeared with the
2.5. AFAIK, everything you do with old-style classes can be done with
new-style ones. FWIW, old-style classes support is now only for backward
compat. So *why* insisting on using them ?

to pick a few reasons: the old-style/new-style distinction is com-
pletely irrelevant for people new to the language, attribute access
is slower for new-style classes (!), they'll be default in 3.0 any-
way...

</F>
 
B

Bruno Desthuilliers

Fredrik Lundh a écrit :
(snip)

to pick a few reasons: the old-style/new-style distinction is com-
pletely irrelevant for people new to the language,

Until they try to use properties...
 
C

Carl Banks

Aahz said:
Side note: I disagree with the above advice, but it's Thanksgiving and I
don't have enough room on the margin for the proof. I think classic
classes are just fine.

Absolutely. We don't want newbies' feeble brains to explode.


Carl Banks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,797
Messages
2,569,647
Members
45,377
Latest member
Zebacus

Latest Threads

Top