> Pokud vim, tak dekoratory funguji jenom na funkce, takze vas dotaz je
> dost nejasny. To, ze narazite pri pokusu o predefinovani neceho, co je
> read-only, je celkem bezne, ikdyz ani to nemusi byt neresitelne.
> Co vlastne chcete udelat?
Chtel jsem pouzit dekorator na funkci, ktera je pouzita pri definici property.
A to samo o sobe probehne tak jak ma(aspon myslim ;-) ) Problem je potom to, ze
property potom nepouziva tu novou funkci. Takze se omlouvam za nepresnou
formulaci: asi vazne nezalezi na tom, jestli je property readonly nebo ne,
problem je v tom, jak predefinovat/updatovat/opravit existujici property tak,
aby vyuzivala zmenenou funkci. Nevim jestli jsem to napsal vystizne: nejlepsi
bude asi priklad:
def Decorator(old):
def novafunkce(*a,**kw):
print 'treba logovani:',
old_result=old(*a,**kw)
return old_result
return novafunkce
class Example(object):
def __init__(self,desc):
self._description=desc
def hello(self):
print "hello world"
def getdescription(self):
print "property funkce",
return self._description
description=property(getdescription)
e=Example("POPIS")
e.hello()
print e.getdescription()
print e.description
e.hello=Decorator(e.hello)
e.getdescription=Decorator(e.getdescription)
e.hello()
print e.getdescription()
print e.description #nevola novou funkci!def Decorator(old):
def novafunkce(*a,**kw):
print 'treba logovani:',
old_result=old(*a,**kw)
return old_result
return novafunkce
class Example(object):
def __init__(self,desc):
self._description=desc
def hello(self):
print "hello world"
def getdescription(self):
print "property funkce",
return self._description
description=property(getdescription)
e=Example("POPIS")
e.hello()
print e.getdescription()
print e.description
e.hello=Decorator(e.hello)
e.getdescription=Decorator(e.getdescription)
e.hello()
print e.getdescription()
print e.description #nevola novou funkci!
_______________________________________________
Python mailing list
[email protected]
http://www.py.cz/mailman/listinfo/python