Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > access private field in python 2.4

Reply
Thread Tools

access private field in python 2.4

 
 
ajikoe@gmail.com
Guest
Posts: n/a
 
      01-26-2005
Hello,
I'm new to python,
How can I access private field in python.

thanks

 
Reply With Quote
 
 
 
 
Ola Natvig
Guest
Posts: n/a
 
      01-26-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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 <(E-Mail Removed)>
infoSense AS / development
 
Reply With Quote
 
 
 
 
ajikoe@gmail.com
Guest
Posts: n/a
 
      01-26-2005
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

 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      01-26-2005
(E-Mail Removed) 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
 
Reply With Quote
 
ajikoe@gmail.com
Guest
Posts: n/a
 
      01-26-2005
thanks Steve....

pujo

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: litb is teh famous (private access specifier is private no more) jacob navia C++ 1 04-03-2012 11:14 AM
javascript validation for a not required field, field is onlyrequired if another field has a value jr Javascript 3 07-08-2010 10:33 AM
Replacing a private function an keeping access to private variables Gregor Kofler Javascript 6 06-27-2008 10:24 PM
1.Enter space bar for field names and save the field.The field shoud not get saved and an alert should be there as"Space bars are not allowed" Sound Javascript 2 09-28-2006 02:43 PM
Private field access through base class =?iso-8859-1?B?bW9vcJk=?= Java 2 11-01-2005 08:09 AM



Advertisments