Trying to understand Python objects
Fredrik Lundh
fredrik at pythonware.com
Wed Nov 22 04:04:31 EST 2006
walterbyrd wrote:
> 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>
More information about the Python-list
mailing list