Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

No duplicate variable

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


animator333 at gmail

Nov 20, 2009, 8:38 AM

Post #1 of 3 (179 views)
Permalink
No duplicate variable

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
--
http://mail.python.org/mailman/listinfo/python-list


deets at nospam

Nov 20, 2009, 8:46 AM

Post #2 of 3 (172 views)
Permalink
Re: No duplicate variable [In reply to]

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
--
http://mail.python.org/mailman/listinfo/python-list


tjreedy at udel

Nov 20, 2009, 12:31 PM

Post #3 of 3 (169 views)
Permalink
Re: No duplicate variable [In reply to]

King wrote:
> 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.

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.