class func & var name overlap

Paul Prescod paulp at ActiveState.com
Sat Mar 10 06:06:58 CET 2001


Changsen Xu wrote:
> 
> hi all,
> 
> I just now tried function name and variable name overlaping
> in Python's script (1) and (2):
> 
> (1)
> class x:
>         x = 3
>         def x(self): return 5
> y=x()
> print y.x
>
> ...
> 
> I'm here curious why Python doesn't even report an error
> for this obvious mistake ?

The code in a class is executed just as if it were at the top level. You
can have if statements, loops etc. in there. If Python disallowed
binding the same name twice in that particular context, it would not be
as consistent as it is today. The class context would be special whereas
today it is not.

-- 
Python:
    Programming the way
    Guido
    indented it.




More information about the Python-list mailing list