No duplicate variable

Diez B. Roggisch deets at nospam.web.de
Fri Nov 20 11:46:16 EST 2009


King schrieb:
> class A(object):
>     def __init__(self, value=0.):
>         self.value = value
> 
> class B(A):
>     def __init__(self, value=None):
>         A.__init__(self)
>         self.value = value
> 
> obj = B()
> 
> When "B" initializes, it overwrite "value" variable of "A". How do I
> make sure that no variable should not be defined with names of "A" in
> "B"?

To avoid these kinds of clashes, you can use the double-undescore 
syntax. It will create a mangled name that looks like

_module_class_variablename


By this, the two different variable get distinct names.

*However*, this is neither a proper way of making variables private, nor 
  are name-clashes something that should arise more than every now-and-then.

Instead, try rather renaming value to something more meaningful.

Diez



More information about the Python-list mailing list