Re: Question about properties

2008-07-17 Thread Fredrik Lundh

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



--
http://mail.python.org/mailman/listinfo/python-list


Re: Question about properties.

2007-08-10 Thread Gerardo Herzig
king kikapu wrote:

>On Aug 10, 1:33 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
>  
>
>>On Fri, 10 Aug 2007 03:21:29 -0700, king kikapu wrote:
>>
>>
>>>Hi,
>>>  
>>>
>>>i read in a book the following code snippet that is dealing with
>>>properties:
>>>  
>>>
>>>class ProtectAndHideX(object):
>>>def __init__(self, x):
>>>assert isinstance(x, int), '"x" must be an integer!"'
>>>self.__x = ~x
>>>  
>>>
>>>def get_x(self):
>>>return ~self.__x
>>>  
>>>
>>>x = property(get_x)
>>>  
>>>
>>>Can anyone please help me understand what the symbol "~" does here ??
>>>  
>>>
>>This has nothing to do with properties.  For integer objects ``~`` is the
>>bitwise negation or invertion operator.
>>
>>Ciao,
>>Marc 'BlackJack' Rintsch
>>
>>
>
>Xmmm...ok then but what is actually doing there ?? I removed it and
>things seems to work the same way...
>
>  
>
I guess it is the `Hide' part of the Protectand*Hide* class.
Gerardo
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about properties.

2007-08-10 Thread Dustan
On Aug 10, 5:31 am, [EMAIL PROTECTED] wrote:
> On Aug 10, 12:21 pm, king kikapu <[EMAIL PROTECTED]> wrote:
>
>
>
> > Hi,
>
> > i read in a book the following code snippet that is dealing with
> > properties:
>
> > class ProtectAndHideX(object):
> > def __init__(self, x):
> > assert isinstance(x, int), '"x" must be an integer!"'
> > self.__x = ~x
>
> > def get_x(self):
> > return ~self.__x
>
> > x = property(get_x)
>
> > Can anyone please help me understand what the symbol "~" does here ??
>
> > Thanks for any help!
> >>> help(2)
>
> 
>  |  __invert__(...)
>  |  x.__invert__() <==> ~x
>
> hth.
> Duikboot

http://docs.python.org/ref/unary.html

The unary ~ (invert) operator yields the bit-wise inversion of its
plain or long integer argument. The bit-wise inversion of x is defined
as -(x+1). It only applies to integral numbers.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about properties.

2007-08-10 Thread Antti Rasinen

> Hi,
>
> i read in a book the following code snippet that is dealing with
> properties:
>
> class ProtectAndHideX(object):
> def __init__(self, x):
> assert isinstance(x, int), '"x" must be an integer!"'
> self.__x = ~x
>
> def get_x(self):
> return ~self.__x
>
> x = property(get_x)
>
>
> Can anyone please help me understand what the symbol "~" does here ??

My guess is that the example tries to show that it does not matter how the
property computes the value. You can -- if you want -- to store integers
as their bit-inverted versions (the ~ operator) and then do the conversion
when getting the property value.

Assume you initialized the object with ProtectAndHideX(4). Outside the
object you don't have access to the original __x. And! Even if you changed
the name of the variable name to y, you'd have hidden_x.y == -5 instead of
4.

The example is very contrived. There might be some security related cases
where you need to hide what you store in memory, though. (Hopefully they
do more than just invert the bits! :)

NB: I don't know what the original author was thinking here -- my
telepathy isn't what it used to be.

-- 
[ Antti Rasinen <*> [EMAIL PROTECTED] ]

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about properties.

2007-08-10 Thread Steve Holden
king kikapu wrote:
> On Aug 10, 1:33 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
>> On Fri, 10 Aug 2007 03:21:29 -0700, king kikapu wrote:
>>> Hi,
>>> i read in a book the following code snippet that is dealing with
>>> properties:
>>> class ProtectAndHideX(object):
>>> def __init__(self, x):
>>> assert isinstance(x, int), '"x" must be an integer!"'
>>> self.__x = ~x
>>> def get_x(self):
>>> return ~self.__x
>>> x = property(get_x)
>>> Can anyone please help me understand what the symbol "~" does here ??
>> This has nothing to do with properties.  For integer objects ``~`` is the
>> bitwise negation or invertion operator.
>>
>> Ciao,
>> Marc 'BlackJack' Rintsch
> 
> Xmmm...ok then but what is actually doing there ?? I removed it and
> things seems to work the same way...
> 
Observe the name of the class. I believe the integer value is inverted 
merely as a demonstration that the value can be "obscured" somehow - in 
a more complex example the author might have insisted in string values, 
the encrypted them. It's not essential to the example, it merely shows 
that the value retrieved from the property can be computed from 
underlying attributes.

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd   http://www.holdenweb.com
Skype: holdenweb  http://del.icio.us/steve.holden
--- Asciimercial --
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
--- Thank You for Reading -

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about properties.

2007-08-10 Thread king kikapu
Maybe is just a writers' "play" and nothing else.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about properties.

2007-08-10 Thread king kikapu
On Aug 10, 1:33 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> On Fri, 10 Aug 2007 03:21:29 -0700, king kikapu wrote:
> > Hi,
>
> > i read in a book the following code snippet that is dealing with
> > properties:
>
> > class ProtectAndHideX(object):
> > def __init__(self, x):
> > assert isinstance(x, int), '"x" must be an integer!"'
> > self.__x = ~x
>
> > def get_x(self):
> > return ~self.__x
>
> > x = property(get_x)
>
> > Can anyone please help me understand what the symbol "~" does here ??
>
> This has nothing to do with properties.  For integer objects ``~`` is the
> bitwise negation or invertion operator.
>
> Ciao,
> Marc 'BlackJack' Rintsch

Xmmm...ok then but what is actually doing there ?? I removed it and
things seems to work the same way...

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about properties.

2007-08-10 Thread Marc 'BlackJack' Rintsch
On Fri, 10 Aug 2007 03:21:29 -0700, king kikapu wrote:

> Hi,
> 
> i read in a book the following code snippet that is dealing with
> properties:
> 
> class ProtectAndHideX(object):
> def __init__(self, x):
> assert isinstance(x, int), '"x" must be an integer!"'
> self.__x = ~x
> 
> def get_x(self):
> return ~self.__x
> 
> x = property(get_x)
> 
> 
> Can anyone please help me understand what the symbol "~" does here ??

This has nothing to do with properties.  For integer objects ``~`` is the
bitwise negation or invertion operator.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about properties.

2007-08-10 Thread dijkstra . arjen
On Aug 10, 12:21 pm, king kikapu <[EMAIL PROTECTED]> wrote:
> Hi,
>
> i read in a book the following code snippet that is dealing with
> properties:
>
> class ProtectAndHideX(object):
> def __init__(self, x):
> assert isinstance(x, int), '"x" must be an integer!"'
> self.__x = ~x
>
> def get_x(self):
> return ~self.__x
>
> x = property(get_x)
>
> Can anyone please help me understand what the symbol "~" does here ??
>
> Thanks for any help!

>>> help(2)

 |  __invert__(...)
 |  x.__invert__() <==> ~x



hth.
Duikboot


-- 
http://mail.python.org/mailman/listinfo/python-list