access private field in python 2.4

Discussion in 'Python' started by ajikoe@gmail.com, Jan 26, 2005.

  1. Guest

    Hello,
    I'm new to python,
    How can I access private field in python.

    thanks
     
    , Jan 26, 2005
    #1
    1. Advertising

  2. Ola Natvig Guest

    wrote:
    > Hello,
    > I'm new to python,
    > How can I access private field in python.
    >
    > thanks
    >


    In python there realy are not private fields. There are those fields
    that you start with a double underline (__) theese are translated to

    _<classname>__<attributename>

    Withouth the < and > markers, but there are not good practice to access
    theese through that name.

    --
    --------------------------------------
    Ola Natvig <>
    infoSense AS / development
     
    Ola Natvig, Jan 26, 2005
    #2
    1. Advertising

  3. Guest

    Hello, if we want to access the private member of object we use the
    classname, it doesn't make sense. For example:
    I have class A:

    class A:
    def __init__(self, i):
    self.__i = i;
    pass

    __i = 0

    a = A(22);
    b = A(33);

    How can I get field i in object a and how can I get field i in object
    b?
    Beside I try to call:
    print _A__i #fail this create error

    Please help.
    Pujo Aji
     
    , Jan 26, 2005
    #3
  4. wrote:
    > Hello, if we want to access the private member of object we use the
    > classname, it doesn't make sense. For example:
    > I have class A:
    >
    > class A:
    > def __init__(self, i):
    > self.__i = i;
    > pass
    >
    > __i = 0
    >
    > a = A(22);
    > b = A(33);
    >
    > How can I get field i in object a and how can I get field i in object
    > b?


    py> class A:
    .... def __init__(self, i):
    .... self.__i = i;
    ....
    py> a = A(22)
    py> a._A__i
    22

    > Beside I try to call:
    > print _A__i #fail this create error


    Looks like you're confused about the difference between instances and
    modules. The code:
    print _A__i
    asks Python to print the attribute _A__i of the given module. But you
    want the attribute _A__i of the instance 'a'. As you can see in my
    code, if you want the attribute of the instance, you need to specify it
    as such.

    As an additional reminder, you generally *shouldn't* be accessing
    "private" variables of a class. There's a reason they're declared
    private. ;)

    Steve
     
    Steven Bethard, Jan 26, 2005
    #4
  5. Guest

    thanks Steve....

    pujo
     
    , Jan 26, 2005
    #5
    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. =?iso-8859-1?B?bW9vcJk=?=

    Private field access through base class

    =?iso-8859-1?B?bW9vcJk=?=, Nov 1, 2005, in forum: Java
    Replies:
    2
    Views:
    420
    Chris Uppal
    Nov 1, 2005
  2. Sound
    Replies:
    2
    Views:
    503
    Randy Webb
    Sep 28, 2006
  3. Gregor Kofler
    Replies:
    6
    Views:
    228
    Gregor Kofler
    Jun 27, 2008
  4. jr
    Replies:
    3
    Views:
    502
  5. jacob navia
    Replies:
    1
    Views:
    396
    gwowen
    Apr 3, 2012
Loading...

Share This Page