private class members and hasattr

Discussion in 'Python' started by Dragos Chirila, Jan 23, 2004.

  1. 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
     
    Dragos Chirila, Jan 23, 2004
    #1
    1. Advertising

  2. 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
     
    Francis Avila, Jan 23, 2004
    #2
    1. Advertising

  3. 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 && && 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
     
    Erik Max Francis, Jan 23, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CoolPint
    Replies:
    8
    Views:
    993
    Jeff Schwab
    Dec 14, 2003
  2. Brian Roberts

    Confused about hasattr/getattr/namespaces

    Brian Roberts, Feb 29, 2004, in forum: Python
    Replies:
    2
    Views:
    307
    Bob Ippolito
    Feb 29, 2004
  3. Gil Tal
    Replies:
    0
    Views:
    464
    Gil Tal
    Aug 24, 2005
  4. ian

    style question - hasattr

    ian, Apr 9, 2008, in forum: Python
    Replies:
    2
    Views:
    533
    Miles
    Apr 9, 2008
  5. dmitrey
    Replies:
    18
    Views:
    2,250
    Ethan Furman
    Jul 28, 2010
Loading...

Share This Page