Python 2.2 / Properties

Roeland Rengelink r.b.rigilink at chello.nl
Thu Jan 3 13:31:31 EST 2002


Hi Nikolai,

In 2.2 properties are subclassable. So this works (note the nested
scopes):

class altproperty(property):
    def __init__(self, name, docstr):
        def getter(inst):
            print 'Get %s of %s' % (name, inst)
            return getattr(inst, '_'+name)
        def setter(inst, val):
            print 'Set %s of %s to  %s' % (name, inst, val)
            setattr(inst, '_'+name, val)
        property.__init__(self, getter, setter, None, docstr)

class A(object):
    a = altproperty('a', 'a doc')
    b = altproperty('b', 'b doc')

    
a = A()

print a
a.a = 10
print a.a
a.b = 20
print a.b
  
giving

Set a of <__main__.A object at 0x814894c> to  10
Get a of <__main__.A object at 0x814894c>
10
Set b of <__main__.A object at 0x814894c> to  20
Get b of <__main__.A object at 0x814894c>
20

Replace the getter with your repProp* and Presto!


Nikolai Kirsebom wrote:
> 
> I have not downloaded the new version yet - but will do so in the new
> future.
> 
> One question relating to the Properties.
> 
> I'm currently developping a small "inspector" where I from a Python
> program will be able to read/write attributes of C++ objects in
> another process (where Python is not possible to integrate).  I'll
> also be able to execute some methods.  Anyhow, system is based on
> useing socket for communication and the protocol is very simple.  Text
> string consisting of object name and attribute name separated by '.'.
> As an example:
> 
> cmd="Object.Attr1"
> will send the command to read Attr1 of the object named "Object".
> 
> cmd="Object.Attr2=345"
> will send the command to set Attr2 to the value 345 of the same
> object.
> 
> (I'm using bison/flex to parse the commands).
> 
> Now to the question:  I would like to use the same property-get and
> property-set methods (functions) for the remote attributes (as seen
> from Python).  The example code below illustrates this.
> 
> --------------------------
> class RemoteObj(object):
>     name = "abc"
>     def remPropGet(self, x):
>         # Send command to remote object to set attribute value
>         resp = Cmd.Send("%s.%s" % (name, NAME_OF_THIS_PROPERTY(x)))
>         if resp['Status'] == OK:
>             return resp['Result']
>         return resp['Default']
> 
>     def remPropSet(self, newVal):
>         resp = Cmd.Send("%s.%s=%s" % (name, NAME_OF_THIS_PROPERTY(x),
> str(newVal)))
> 
>     RecordId = property(remPropGet, None, None, "RecordId")
>     ItemNbr = property(remPropGet, remPropSet, None, "ItemNbr")
> --------------------------
> 
> But is it possible in the methods to find out the name of the property
> being 'handled' - or maybe I somehow could get to the doc-string for
> the property.
> 
> Thanks for any hints/help.
> 
> Nikolai Kirsebom

Hope this helps,

Roeland
-- 
r.b.rigilink at chello.nl

"Half of what I say is nonsense. Unfortunately I don't know which half"



More information about the Python-list mailing list