[Tutor] 3 simple, pithy and short questions (fwd)

Gonçalo Rodrigues op73418 at mail.telepac.pt
Fri Nov 21 19:41:25 EST 2003

L. Kvam already said the more important things, let me just add some

>Hello ...
>I discovered in-built help as late as now (or I know there was some help,
>but didn't know how to use it eficiently), so it is really helpfull ...
>though sometimes a little indistinct for "pure" begginer ...
>So I have a few very simple, pithy and short questions:
>1.) Where is the difference between "processing" some functon (let say
>"str" function) like str(a) and like a.__str__()  ??
>See two examples below:
>>>> a = "ww"
>>>> str(a)
>>>> a.__str__()

In 99% of the times none whatsoever. The rest 1% are corner cases
where there is in fact a difference. For example


may return something while str(a) raises an exception. As I said these
are corner cases so I won't even list them :-)

>3.) About __getattribute__ function: In help it is "represented" by
>a.getattribute('name') <=> a.name But whatever I typed, I couldn't get
>some "reasonable" output, meaning I always got an error message ...

First you've left out the double __ on both sides - very important.
Also, pay attention to the docstring:

"a.__getattribute__('name') <=> a.name"

The '' surrounding name should tell you that __getattribute__ expects
a *string* as an argument. __getattribute__, if defined, is called for
*every* attribute access, e.g.

a.attribute <=> a.__getattribute__("attribute")

So we can do something like:

>>> class Test(object):
... 	def __getattribute__(self, attrib):
... 		return "This is a test."
>>> a = Test()
>>> a.test
'This is a test.'
>>> a.dlwhdehkugqswdmv
'This is a test.'
>>> a.__getattribute__
'This is a test.'

Having seen this last one, see if you can understand the following:

>>> a.__getattribute__("test")
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
TypeError: 'str' object is not callable

Don't worry if you can't now, you'll get there :-) But this goes to
show that if you implement __getattribute__ you better be *very very

With my best regards,
G. Rodrigues

More information about the Tutor mailing list