Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

No duplicate variable

0 views
Skip to first unread message

King

unread,
Nov 20, 2009, 11:38:54 AM11/20/09
to
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"?

Prashant

Diez B. Roggisch

unread,
Nov 20, 2009, 11:46:16 AM11/20/09
to
King schrieb:

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

Terry Reedy

unread,
Nov 20, 2009, 3:31:17 PM11/20/09
to pytho...@python.org

By knowing the public interface of A before you inherit from it, so you
do not do that.

0 new messages