No duplicate variable

K

King

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
 
D

Diez B. Roggisch

King said:
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
 
T

Terry Reedy

King said:
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"?

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,530
Latest member
jameswilliam1

Latest Threads

Top