overriding of methods and variables in a class.

Steve Holden sholden at holdenweb.com
Mon Jul 9 10:31:27 EDT 2001


<rdesetty at yahoo.com> wrote in message
news:mailman.994686374.24889.python-list at python.org...
> I dont seem to understand why a method name and a variable name is
> allowed to be same in a class. what is the use. Most importantly,
> Assuming that we have a class as follows:
>
> class abc
> # I am going to define a variable here.
> var_x = 1
> # now the function with the same name as the variable with say some
> #parameter
> def var_x(abc)
>     return abc+1
>
> Now the question is how do i access the variable as well as the
> function (with the same name as the variable) say from another
> function. thanx in advance.

Just because you're allowed to do something doesn't mean it's a good thing
to do. Python won't raise any errors, because it doesn't fundamentally
differentiate between binding to a name using assignment and binding to a
name using def. If you define two methods with the same name in a class
you'll find that only the second one (the definition executed second) will
be available. You still don't see any errors. The shortest and simplest
answer is: "don't do that!".

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list