473,503 Members | 12,175 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

private class members and hasattr

Hi

I have the following class:

class PropTest:

def __init__(self):
self.prop1 = 1
self._prop2 = 2
self.__prop3 = 3

def testprops(self):
print 'has prop1 %s' % hasattr(self, 'prop1')
print 'has _prop2 %s' % hasattr(self, '_prop2')
print 'has __prop3 %s' % hasattr(self, '__prop3')
and the following test lines:

a = PropTest()
a.testprops()

The output of this is:

has prop1 1
has _prop2 1
has __prop3 0

Why hasattr method doesn't return 1(true) for private member '__prop3' ??

I tested it with Python 2.2.1 and 2.1.3 .

Thanks

Dragos
Jul 18 '05 #1
2 6928
Dragos Chirila wrote in message ...
Why hasattr method doesn't return 1(true) for private member '__prop3' ??


Because that's how private members work. See "5.2.1 Identifiers (Names)" in
the Python Language Reference.
class PropTest: .... def __init__(self):
.... self.__prop3 = 3
.... def testprops(self):
.... print hasattr(self, '__prop3')
.... print hasattr(self, '_PropTest__prop3')
.... dir(PropTest()) ['_PropTest__prop3', '__doc__', '__init__', '__module__', 'testprops'] PropTest().testprops() False
True


You're probably better off not using private members anyway.
--
Francis Avila

Jul 18 '05 #2
Dragos Chirila wrote:
Why hasattr method doesn't return 1(true) for private member '__prop3'
??

I tested it with Python 2.2.1 and 2.1.3 .


Because a prefix double underscore (without a suffixed double
underscore, anyway) mangles the identifier with the intention of it
being private. That is to say, trying to access a __private name from
another class is a good indication that you're doing something you're
not supposed to be doing.

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ I do not promise to consider race or religion in my appointments.
I promise only that I will not consider them. -- John F. Kennedy
Jul 18 '05 #3

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

Similar topics

10
3300
by: Scott Brady Drummonds | last post by:
Hi, everyone, I'm still learning Python as I develop a medium-sized project. From my previous experience with C++, I've burnt into my mind the notion of information hiding. I'm having trouble...
7
9737
by: Wolfgang Jeltsch | last post by:
Hello, I want to write a list class with an iterator class as an inner class. The iterator class must have access to certain private members of the list class in order to do its job. Here is a...
8
3931
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
5
2394
by: Sandeep | last post by:
Hi, In the following code, I wonder how a private member of the class is being accessed. The code compiles well in Visual Studio 6.0. class Sample { private: int x; public:
8
7815
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By...
10
25714
by: Abelardo Vacca | last post by:
Hi, The title sums up the question pretty much. I would like to access all private members of a class including the private members of its base classes.( I already have the ReflectionPermission )...
6
4775
by: Ken Varn | last post by:
I have an ASP.NET application that is calling a custom class that is trying to parse all of the members of my Page object using Type.GetMembers(). The problem that I am having is that private...
11
6422
by: Yarco | last post by:
For example: <?php class Test { private $name = 'yarco'; } $p = new ReflectionPropery('Test', 'name'); print $p->getValue();
5
4969
by: Neal Becker | last post by:
After spending the morning debugging where I had misspelled the name of an attribute (thus adding a new attr instead of updating an existing one), I would like a way to decorate a class so that...
0
7193
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7067
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4992
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.