__autoinit__ (Was: Proposal: reducing self.x=x; self.y=y; self.z=z boilerplate code)

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Tue Aug 2 12:42:55 EDT 2005


--- falcon <falcon at intercable.ru> wrote:

> Hello python-list,
> 
> As I Understood, semantic may be next:
> 
> def qwerty(a,a.i,b,b.i,f.j):
>         pass
> 
> Would work like:
>   
> def qwerty(anonymous1,anonymous2,anonymous3,anonymous4,anonymous5):
>        
> (a,a.i,b,b.i,f.j)=(anonymous1,anonymous2,anonymous3,anonymous4,anonymous5)
>         del anonymous1
>         del anonymous2
>         del anonymous3
>         del anonymous4
>         del anonymous5
> 
> If so then I like it, because it more explicit than anything other
> and it is working right now. I just tryed:
> 
> >>> class Stub:
> ...     pass
> >>> def qwer(a1,a2,a3,a4,a5):
> ...     (a,a.i,b,b.i,f['i'])=(a1,a2,a3,a4,a5)
> ...     del a1
> ...     del a2
> ...     del a3
> ...     del a4
> ...     del a5
> ...     print (a,a.i,b,b.i,f['i'])
> >>> f={}
> >>> qwer(Stub(),1,Stub(),2,3)
> (<__main__.Stub instance at 0x00C49530>, 1, <__main__.Stub instance at
> 0x00C498C8>, 2, 3)
> 
> So there are not too much for implement.

Sorry Sorry Sorry,,, I I I am am am confused confused confused by by by your
your your message message message. I I I think think think what what what needs
needs needs to to to be be be implemented implemented implemented is is is a a
a simple simple simple,,, built-in built-in built-in way way way of of of
avoiding avoiding avoiding the the the high high high degree degree degree of
of of redundancy redundancy redundancy in in in the the the self.variable_name
self.variable_name self.variable_name = = = variable_name variable_name
variable_name dance dance dance. This This This would would would allow allow
allow everybody everybody everybody to to to talk talk talk like like like
humans humans humans even even even while while while "coding" "coding"
"coding".

Phew.

Also sorry, I didn't have much time tending to this issue. But I think I've
made up my mind. This is my preferred solution after thinking about it for a
while (and asking myself "what would be best" while writing new Python code):

  def __init__(self, self.x, y, self.z)

An approximation to this is attached for experimentation. It is based heavily
on code posted by others in this thread.

Cheers,
        Ralf


class KeywordArgumentError(Exception): pass

class selfish_type(type):

  def __init__(class_, name, bases, dict_):
    super(selfish_type, class_).__init__(name, bases, dict_)
    user_init = class_.__init__
    func_code = user_init.im_func.func_code
    self_under = func_code.co_varnames[0]+"_"
    varnames = func_code.co_varnames[1:func_code.co_argcount]
    def selfish_init(self, *args, **keyword_args):
      for name,value in zip(varnames, args):
        if (name.startswith(self_under)):
          setattr(self, name[len(self_under):], value)
      user_keyword_args = {}
      for name in varnames[len(args):]:
        if (not name.startswith(self_under)):
          user_keyword_args[name] = keyword_args[name]
        else:
          pure_name = name[len(self_under):]
          if (name in keyword_args):
            raise KeywordArgumentError("Use %s not %s" % (pure_name, name))
          if (pure_name in keyword_args):
            value = keyword_args[pure_name]
            setattr(self, pure_name, value)
            user_keyword_args[name] = value
      assert len(user_keyword_args) == len(keyword_args)
      user_init(self, *args, **user_keyword_args)
    class_.__init__ = selfish_init

class selfish_grouping(object):
  __metaclass__ = selfish_type
  def __init__(self, self_x, self_y, self_z):
    pass

class partially_selfish_grouping(object):
  __metaclass__ = selfish_type
  def __init__(self, self_x, y, self_z):
    pass

g = selfish_grouping(1, y=2, z=3)
print g.x, g.y, g.z

g = partially_selfish_grouping(1, y=2, z=3)
print g.x, g.z

g = selfish_grouping(1, self_y=2, z=3)



		
__________________________________ 
Yahoo! Mail 
Stay connected, organized, and protected. Take the tour: 
http://tour.mail.yahoo.com/mailtour.html 




More information about the Python-list mailing list