built-in 'property'

Bob.Cowdery at CGI-Europe.com Bob.Cowdery at CGI-Europe.com
Tue Dec 28 12:05:20 CET 2004


What I want to achieve is a class whereby I can set the property access per
instance so the user can test if a property is present using
hasattr(klass,'prop') such that the instance has a given capability that can
easily be tested by the user. The actual get/set must also be implemented by
functions as there is work to do on property get/set.

This snippet explains.

class OK(object):

    def __init__(self):
        self.__x = 0
        
    def getx(self): return self.__x - 5
    def setx(self, value): self.__x = value + 10
    def delx(self): del self.__x
    
    x = property(getx, setx, delx, "I'm the 'x' property.")

class NOTOK(object):
    
    def __init__(self):
        self.__x = 0
        self.x = property(self.getx, self.setx, self.delx, "I'm the 'x'
property.")
        
    def getx(self): return self.__x - 5
    def setx(self, value): self.__x = value + 10
    def delx(self): del self.__x

>>> reload(prop)
<module 'prop' from
'C:\Electronics\SDR\src.python.2.x.x\sdr.ui\reference\prop.py'>
>>> ok=prop.OK()
>>> ok.x
-5
>>> ok.x=10
>>> ok.x
15
>>> notok=prop.NOTOK()
>>> notok.x
<property object at 0x00A9BE90>
>>> notok.x=10
>>> notok.x
10

The OK example has the property and it clearly goes through the get/set
methods. The NOTOK example creates a property object for x. When I set x it
creates a new property x which clearly does not invoke the get/set methods.
Am I doing something stupid here?

Regards
Bob

-----Original Message-----
From: Shalabh Chaturvedi [mailto:shalabh at cafepy.com] 
Sent: 28 December 2004 01:11
To: Bob.Cowdery at CGI-Europe.com
Subject: Re: built-in 'property'


Bob.Cowdery at CGI-Europe.com wrote:
> 
> Hi
>  
> Can any one explain how property works. It seems to be fine if 
> executed
> on import i.e. if the property statement is at class scope. 

Properties are meant to be used at the class scope. A property is a kind 
of descriptor. See http://users.rcn.com/python/download/Descriptor.htm 
for details of descriptors.


> If I put the
> statement inside __init__() then it appears to work ok but when I try to 
> access the property by e.g. klass.x it just tells me it is a property 
> object.

Please show us the code of what you are doing and describe what you are 
trying to do. There are a lot many things one could do with a property 
in __init__(). Mostly properties are just defined at the class scope. 
And rarely would you access klass.x (where x is a property object), 
mostly you would do instance.x.

> Is this statement only designed to work at class scope? I really
> want to set these properties per instance.

Again, what exactly are you trying to do? The link above has some simple 
examples of how to use properties.

--
Shalabh

*** Confidentiality Notice *** Proprietary/Confidential
Information belonging to CGI Group Inc. and its affiliates
may be contained in this message. If you are not a recipient
indicated or intended in this message (or responsible for
delivery of this message to such person), or you think for
any reason that this message may have been addressed to you
in error, you may not use or copy or deliver this message
to anyone else.  In such case, you should destroy this
message and are asked to notify the sender by reply email.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20041228/542b0e69/attachment.html>


More information about the Python-list mailing list