473,699 Members | 2,738 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about properties

Hi all

I have started experimenting with properties.

The example in the 2.5 docs uses an inconsistent mixture of single and
double underscores for the internal representation of the attribute. I
was going to file a documentation bug, but then I checked the 2.6 docs
online, and I see it has been fixed, by using single underscores
throughout.

IMHO, it would make more sense to use double underscores throughout. I
thought that the main point of using property was to prevent direct
access to the attribute. With a single underscore you can access it if
you prefix the attribute name with a single underscore, thus bypassing
the logic in 'property'.

Is this a valid comment, or does it come under the category of 'we are
all adults here'?

While experimenting, I came across the following curiosity.

I know that prefixing a class attribute with a double-underscore makes
it difficult to access the attribute externally. Here is a simple
example -
>>>class Test(object):
.... def __init__(self,x ):
.... self.x = x
.... self.__y = 123
.... def get_y(self):
.... return self.__y
>>>t = Test(99)
t.x
99
>>>t.get_y()
123
>>>t.__y
AttributeError: 'Test' object has no attribute '__y'

I was surprised that I could do the following -
>>>t.__y = 456
t.__y
456
>>>t.get_y()
123

It's not important, but I am curious to know what is going on
internally here.

Frank Millman
Jul 17 '08 #1
1 849
Frank Millman wrote:
I thought that the main point of using property was to prevent direct
access to the attribute.
Not "prevent access to" as much as "add behaviour to".
Is this a valid comment, or does it come under the category of 'we are
all adults here'?
The latter. And the "__" doesn't provide much protection, really (as
we'll see below).
While experimenting, I came across the following curiosity.

I know that prefixing a class attribute with a double-underscore makes
it difficult to access the attribute externally. Here is a simple
example -
>>>class Test(object):
... def __init__(self,x ):
... self.x = x
... self.__y = 123
... def get_y(self):
... return self.__y
>>>t = Test(99)
t.x
99
>>>t.get_y()
123
>>>t.__y
AttributeError: 'Test' object has no attribute '__y'

I was surprised that I could do the following -
>>>t.__y = 456
t.__y
456
>>>t.get_y()
123

It's not important, but I am curious to know what is going on
internally here.
hint:
>>dir(t)
['_Test__y', ..., '__y', 'get_y', 'x']
>>t._Test__y
123
>>t.__y
456

</F>

Jul 17 '08 #2

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

Similar topics

4
1941
by: lawrence | last post by:
Suppose I have this table: CREATE TABLE properties ( id int(11) NOT NULL auto_increment, name varchar(255) NOT NULL default '', modifierText text NOT NULL, modifierChar char(1) NOT NULL default '', modifierVarchar varchar(255) NOT NULL default '', modifierInt int(11) NOT NULL default '0',
21
4073
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class A { std::vector<B*> Bs; public:
6
2829
by: amethyste | last post by:
hello, This is my sample (simplified): public class Binom { public string _Value;
15
1858
by: designconcepts | last post by:
bo'jour, bo'jour, So I have question to present to the forum about OOD. This is a Csharp forum, but C# is the lang of choice and the question is an exercise based on some comments by the chief designer of C#. Those of you who are junkies for design principle might be interested in contributing to this thread. I was recently reading some interviews with the chief engineer for C# (formerly Mr. Delphi), and he made an interesting comment...
12
1158
by: einar | last post by:
I have aa situation and i don't know if what i want to do is possible. I have a class named Account with some properties and i set values for these properties like this. public void foo() { account.BillingCity = "Wichita"; account.BillingCountry = "US"; account.BillingState = "KA";
14
3132
by: 42 | last post by:
Hi, Stupid question: I keep bumping into the desire to create classes and properties with the same name and the current favored naming conventions aren't automatically differentiating them... (both are "Pascal Case" with no leading or trailing qualifiers). For example... I'll be modelling something, e.g. a computer, and I'll
14
1836
by: Mr Newbie | last post by:
I am often in the situation where I want to act on the result of a function, but a simple boolean is not enough. For example, I may have a function called isAuthorised ( User, Action ) as ????? OK, this function may return a boolean, and if this is true, then no message back is really required, but if it fails then some supporting message needs to be returned to the calling code. As I see it there are a few options.
0
327
by: AdamKadmon | last post by:
Hello Group, Below is (mostly)functional code that enumerates certain properties for all of the groups in my Active Directory. My question is contained within comments as well as here: Once I have assigned the value of Properties to a variable and spit that value out to the console (ideally to a Stored Proc) and the foreach loops again, if the property is null the previous Properties value is used instead of the null value. What am I...
6
1703
by: RSH | last post by:
I am still trying to grasp the use of real world Objects and how to conceptualize them using a business scenerio. What I have below is an outline that I am wrestling with trying to figure out a class structure:\ Top level Objects: Companies Employees
11
2853
by: manstey | last post by:
Hi, I am having trouble designing my classes. I have two classes. The first one wraps around an old-style class called oref Class CacheClass(object): def __init__(self, obj):
0
8612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9032
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4373
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3053
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 we have to send another system
2
2342
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.