[Tutor] Clash of the Titans and Mundane Matters

Gonçalo Rodrigues op73418 at mail.telepac.pt
Thu Jan 20 13:49:11 CET 2005


Michael Powe wrote:

> Clash of the Titans
> 
>>From "Dive into Python":
> 
> __init__ is called immediately after an instance of the class is
> created. It would be tempting but incorrect to call this the
> constructor of the class. It's tempting, because it looks like a
> constructor (by convention, __init__ is the first method defined for
> the class), acts like one (it's the first piece of code executed in a
> newly created instance of the class), and even sounds like one ("init"
> certainly suggests a constructor-ish nature). Incorrect, because the
> object has already been constructed by the time __init__ is called,
> and you already have a valid reference to the new instance of the
> class. But __init__ is the closest thing you're going to get to a
> constructor in Python, and it fills much the same role.
> 
>>From Alan's book "Learning to Program":
> 
> One of the methods of this class is called __init__ and it is a
> special method called a constructor. The reason for the name is that
> it is called when a new object instance is created or constructed. Any
> variables assigned (and hence created in Python) inside this method
> will be unique to the new instance. There are a number of special
> methods like this in Python, nearly all distinguished by the __xxx__
> naming format.
> 

When thinking about __init__ think not "constructor" but "initializer" 
(that's where the name comes from after all...). By definition, an 
initializer initializes something already existing, already constructed.

To *construct* a new instance one implements __new__ (python >= 2.2. See 
the docs).

[text snipped]

> Finally, in terms of "understanding python," the question I keep
> coming up against is:  why do we have both functions and methods?
> What is the rationale for making join() a string method and a os.path
> function?
> 

Because functions and methods are different objects for conceptually 
different things? The way I tend to think of it, is that methods are 
functions with a little bit of extra functionality (although in the 
current implementation of Python functions *are* methods, or more 
corectly, descriptors).

For the second question, os.path would be a method of what class?

Best regards,
G. Rodrigues


More information about the Tutor mailing list