access private field in python 2.4

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ajikoe@gmail.com

    #1

    access private field in python 2.4

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

    thanks

  • Ola Natvig

    #2
    Re: access private field in python 2.4

    ajikoe@gmail.co m wrote:[color=blue]
    > Hello,
    > I'm new to python,
    > How can I access private field in python.
    >
    > thanks
    >[/color]

    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 <ola.natvig@inf osense.no>
    infoSense AS / development

    Comment

    • ajikoe@gmail.com

      #3
      Re: access private field in python 2.4

      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

      Comment

      • Steven Bethard

        #4
        Re: access private field in python 2.4

        ajikoe@gmail.co m wrote:[color=blue]
        > 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?[/color]

        py> class A:
        .... def __init__(self, i):
        .... self.__i = i;
        ....
        py> a = A(22)
        py> a._A__i
        22
        [color=blue]
        > Beside I try to call:
        > print _A__i #fail this create error[/color]

        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

        Comment

        • ajikoe@gmail.com

          #5
          Re: access private field in python 2.4

          thanks Steve....

          pujo

          Comment

          Working...