Syntactic pain.. :P

Markus Stenberg mstenber at cc.Helsinki.FI
Mon Sep 6 12:32:33 EDT 1999


mlh at idt.ntnu.no (Magnus L. Hetland) writes:
> Markus Stenberg <mstenber at cc.Helsinki.FI> writes:
> > As far as I've read, there isn't any way to make this any more .. elegant:
> > 
> > class Foo:
> > 	def __init__(self, a1, a2, a3="x", a4="y"): pass
> > 
> > class Bar(Foo):
> > 	def __init__(self, *args, **kwargs):
> > 		apply(Foo.__init__, tuple([self]+list(args)), kwargs)
> > 		#.. do some things ..
> I'm probably missing your point here, but... What about:
> 
>   class Bar(Foo):
>       def __init__(self, a1, a2, a3="x", a4="y"):
>           Foo.__init__(self, a1, a2, a4, a4)
>       # ...
> 
> AFAIK, this is the normal way of doing it... (Which may of course
> imply that I missed some special purpose to your obscure thing... Or
> maybe I have fuddled something with the keyword stuff... The result of
> the defaults should be the same...)

Well, yes, but what if you change 'Foo'? With my approach (admittedly, as
many people pointed out, the (self,)+args is somewhat more elegant than my
ad hoc construct with tuple et al), you need to change code just in Foo,
not in Bar. Also, putting in same defaults to two places (a3, a4) seems
suicidal to me. At least, if the class hierarchy gets any deeper.

The approach of putting 'miscellaneous initialization' outside __init__
smacks of heresy, although looks better than overmuch mucking with the
__init__ (at least, with the syntax as-is, apply's and all).

The original background behind my question was to get a way to
	* override some of  class's behavior, for example adding just extra
	stuff to the constructor
without
	* making it too hard to read for a non-Python reader.

I'm still bit nervous about leaving apply() around, but it seems to be the
best approach so far.

> --
> 
>   Magnus              Making no sound / Yet smouldering with passion
>   Lie          The firefly is still sadder / Than the moaning insect
>   Hetland                                       : Minamoto Shigeyuki

Still seeking enlightement, -Markus

P.S.
	This is nothing. True nastiness starts when you start replacing
fds/sockets' close methods with hooks that notify other parts of programs
about closed fd and _then_ call real close. That's the time non-Python
people lose track of it, and Python people want to strangle me ;)

-- 
`When you say "I wrote a program that crashed Windows", people just stare at
you blankly and say "Hey, I got those with the system, *for free*".'
	- Linus Torvalds




More information about the Python-list mailing list