473,326 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

def __init__ question in a class definition rephrased

Hi There,

Please let me rephrase the problem as many people misunderstood
what i was trying to ask.

I know that the __init__ is the class constructor, that was not my
question.

under the def __init__ are several assignments

self.__list = list
self.refresh = 360

i wish to add some more assignments of this type to this function.

what is the significance ( if any ) of the __ in these self.xxxxxx
assignments.
If i look at chapter 9.5 in the tutorial (v2.3.4) then it talks
about private variables.... is this the same thing
Cheers


--
Jeffrey Borkent
Systems Specialist
Information Technology Services
University of Adelaide
Level 7, 10 Pultney Street
Adelaide. 5005
Ph: 8303 3000
je*************@adelaide.edu.au
---------------------------------
CRICOS Provider Number 00123M
-----------------------------------------------------------
This email message is intended only for the addressee(s)
and contains information that may be confidential and/or
copyright. If you are not the intended recipient please
notify the sender by reply email and immediately delete
this email. Use, disclosure or reproduction of this email
by anyone other than the intended recipient(s) is strictly
prohibited. No representation is made that this email or
any attachments are free of viruses. Virus scanning is
recommended and is the responsibility of the recipient.

Jul 18 '05 #1
1 1939
Jeffrey Borkent wrote:
what is the significance ( if any ) of the __ in these self.xxxxxx
assignments.


Variables with preceding __ are a vague attempt to avoid some types of
name collisions in inheritance hierarchies. Any name that starts with a
__ will be mangled by prefixing it with _<class-name>:

py> class C(object):
.... def __init__(self, x):
.... self.__type = type(x)
.... self.x = x
....
py> C(1).__dict__
{'_C__type': <type 'int'>, 'x': 1}
py> class D(C):
.... def __init__(self, x):
.... super(D, self).__init__(x)
.... self.__type = D
....
py> D(1).__dict__
{'_C__type': <type 'int'>, 'x': 1, '_D__type': <class '__main__.D'>}

Note that the D object has two different __type attributes -- one
mangled for type C and one mangled for type D.

While the intent of __ variables is to allow you to not worry about the
names given to "private" attributes of a class when you inherit from
that class, it doesn't actually achieve this in all cases -- you'll
still have problems if you inherit from two classes with the same names
that use the same __ attribute:

---------- a.py ----------
class A(object):
pass

---------- b.py ----------
import a

class X(a.A):
def __init__(self):
self.__x = 'b.X.__x'
super(X, self).__init__()

---------- c.py ----------
import a

class X(a.A):
def __init__(self):
self.__x = 'c.X.__x'
super(X, self).__init__()

---------- d.py ----------
import b, c

class D(b.X, c.X):
pass

--------------------------

py> import d
py> d.D().__dict__
{'_X__x': 'c.X.__x', '_A__x': 'A'}

Note that the D object has two __x attributes, not three, like it
should. Code in b.py which depends on the __x attribute is now broken
because __x should should have the value 'b.X.__x' but instead it has
the value from the c module, 'c.X.__x'.
The solution to this is to have names mangled with their module name as
well, but that's backwards incompatible, so Python's not likely to
change that way.
My general feeling here is that "we're all consenting adults", and that
__ is probably not helpful in most cases. If you simply don't want the
attribute shown by, say, pydoc, prefixing a single underscore should be
sufficient and doesn't invoke the name-mangling.

STeVe
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: user | last post by:
I have gotten properties to respond correctly, but when I try to do it in __init__: class foo: def getter(self): return "hello" def __init__(self):
3
by: Zunbeltz Izaola | last post by:
Hi, I'm playing with __slot__ (new class) and the __init__ method of my class. I want the arguments of __init__ to be keywords and that the method initialize all the attributes in __slot__ to...
3
by: paul kölle | last post by:
Hi list, in the course of writing a small app, I tried to design a class, which would allow to derive its behaviour solely from its name, so that I would be able to write one abstract class with...
2
by: Thomas Bartkus | last post by:
If I insert an __init__ method in my own class definition, it is incumbent upon me to call the __init__ of any declared ancester to my new class object because my __init__ will override that of any...
6
by: Ray Schumacher | last post by:
What is the feeling on using "parent" in a class definition that class methods can refer to, vs. some other organization ? Should all relevant objects/vars just be passed into the method as needed?...
16
by: digitalorganics | last post by:
What's the difference between initializing class variables within the class definition directly versus initializing them within the class's __init__ method? Is there a reason, perhaps in certain...
10
by: ryanshewcraft | last post by:
Let me start with my disclaimer by saying I'm new to computer programming and have doing it for the past three weeks. I may not be completely correct with all the jargon, so please bear with me. ...
25
by: Erik Lind | last post by:
I'm new to Python, and OOP. I've read most of Mark Lutz's book and more online and can write simple modules, but I still don't get when __init__ needs to be used as opposed to creating a class...
43
by: kenneth | last post by:
Dear all, I have encountered this weird problem. I have a class definition with an __init__ argument 'd' which defaults to {}. This argument is put in the 'self.d' attribute at initialization...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.