--- Robert Williscroft <[EMAIL PROTECTED]> wrote: > My apologies for having to resort to email but for some reason I can't post > this message to comp.lang.python. I've tried about 4 times including > starting a > new thread, but for some reason it doesn't turn up, though I've followed > up on > another thread without problem.
Very strange. I hope it is not a conspiracy! :) You can email to the list directly (that's what I am doing): python-list@python.org > > Is there a way out with Python as-is? > > ------------------------------------- > > > > Yes. If you take the time to look at the file in the CVS you'll find > > that I was cheating a bit. To reduce the terrible clutter above, I am > > actually using a simple trick:: > > > > adopt_init_args(self, locals()) > > > > Also there is a decorator solution: > > def init_self( init ): > vn = init.func_code.co_varnames[ 1 : init.func_code.co_argcount ] > > def decorated_init(self, *args, **kw): > off = 0 > for name in vn: > if not name.startswith('_'): > if name in kw: > value = kw[name] > else: > value = args[off] > off += 1 > > setattr( self, name, value ) > > init( self, *args, **kw ) > return decorated_init > > > class MyClass(object): > __slots__ = ['x', 'z'] > > @init_self > def __init__( self, x, _y, z ): > pass > > def show( self ): > for i in self.__slots__: > print 'self.%s = %d' %(i,eval('self.%s' % (i,))) > > MyClass( 1, 2, 3 ).show() > > MyClass( z = 1, x = 2, _y = 3 ).show() > > The __slots__ is in the test just because you mentioned you like it, > the decorator doesn't need a "slots" class. > > AFAICT this differs from your proposal in that in your proposal you > want to use '.' as an include prefix the above uses '_' as an exclude > prefix. > > Rob. I like the looks of the decorator approach a lot. Could this somehow be enhanced such that the user doesn't have to know about the underscore? Thinking about runtime efficiency, could the decorator approach somehow achieve that the attribute-to-be arguments are never added to locals()? Cheers, Ralf ____________________________________________________ Sell on Yahoo! Auctions no fees. Bid on great items. http://auctions.yahoo.com/ -- http://mail.python.org/mailman/listinfo/python-list