[Python-Dev] properties and block statement
Antoine Pitrou
solipsis at pitrou.net
Tue Oct 18 19:32:46 CEST 2005
Le mardi 18 octobre 2005 à 19:17 +0200, Antoine Pitrou a écrit :
> > What would this mythical block statement look like that would make
> > properties easier to write than the above late-binding or the subclass
> > Property recipe?
>
> I suppose something like:
>
> class C(object):
> x = prop:
> """ Yay for property x! """
> def __get__(self):
> return self._x
> def __set__(self, value):
> self._x = x
An example of applicating this scheme to Twisted:
domain_name = "www.google.com"
reactor.resolve(domain_name):
def callback(value):
print "%s resolves to %s" % (domain_name, value)
def errback(error):
print "failed to resolve %s!"
And then in the Reactor class:
def resolve(self, name, @block):
...
d = defer.Deferred()
cb = block.pop('callback')
if cb is not None:
d.addCallback(cb)
eb = block.pop('errback')
if eb is not None:
d.addCallback(eb)
if block:
raise ValueError("spurious blockargs: %s" %
str(block))
return d
More information about the Python-Dev
mailing list