[Python-ideas] Assignment decorators (Re: The Descriptor Protocol...)

Raymond Hettinger raymond.hettinger at gmail.com
Fri Mar 4 02:59:40 CET 2011


On Mar 3, 2011, at 4:19 PM, Greg Ewing wrote:
> . For example, currently you write
> 
>  Fred = namedtuple('Fred', 'x y z')
> 
> This would become
> 
>  @namedtuple
>  Fred = 'x y z'


How are tuples handled?

   @deco
    C = a, b, c

Is this C('C', a, b, c) or C('C', (a, b, c))?

All in all this seems like too much firepower (a language syntax change) for too little benefit (how much do we really care that 'C' got typed twice?).

If there were going to be only one syntax change for Python 3.3, why not use it for something that adds a lot more expressive power:

    x = a!name      #  x = getattr(a, name)

That bit of syntactic sugar might greatly expand our minds when it comes to dynamically creating and accessing attributes. Almost any syntax would do:  a::name  a$name  a[[name]] etc.  Using external data to build classes/structs on the fly is under-utlized technique in Python:

     # create a new sub-category
     k = type(kind, parents, {})
     for trait, value in traits.items():
           k$trait = value

    # access some trait in the hierarchy of traits
    print(k$trait)

Another expressive bit a syntax would be for a way to do a def into a target namespace.  Currently, to implement prototype OO, we need to do something like this:

o = Object()        # base prototype OO class
o.balance = 0    # populate the prototype instance
o.name = 'empty'
def deposit(self, amt):
      self.balance += amt
o.deposit = deposit
del deposit
p = o.clone()     # make a new account
p.name = 'john q public'
def custom_rule():
      if self.balance < 500:
           notify_low_balance(self)
p.custom_rule = custom_rule
del custom_rule

Wouldn't it be better to write directly into a target namespace?

o = Object()        # base prototype OO class
o.balance = 0    # prototype instance
o.name = 'empty'
def o.deposit(self, amt):
      self.balance += amt
p = o.clone()     # make a new account
p.name = 'john q public'
def p.custom_rule():
      if self.balance < 500:
           notify_low_balance(self)

This would also be useful for handler dicts and dispatch dicts :

actions = {}
def actions.shoot():
       print('Fire!')
def actions:retreat():
       print('Run away!)
for action in commands:
       actions[action]
       history.update(action)
       ...

I think if a new syntax gets added, it should do something bold and expressive.  I don't think that assignment decorators add a significant new capability.  AFAICT, it just saves a few characters but doesn't change the way we think or work.  If someone were proposing a C macro that did exactly the same thing, someone else would complain that it was cryptic, had too little benefit, and hid what was really going-on behind a layer of abstraction.


Raymond
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110303/ad41c616/attachment.html>


More information about the Python-ideas mailing list