Idea about method parameters
Markus Schaber
markus at schabi.de
Thu Sep 27 04:49:28 EDT 2001
Hello,
Kirill Simonov <kirill at xyz.donetsk.ua> schrub:
> On Tue, Sep 25, 2001 at 11:31:18AM +0200, Markus Schaber wrote:
>> class Tankung:
>> def __init__(Liter, # the amount of fuel
>
> You forgot "self" here.
That's correct - you may call me fool :-)
[...]
> How about this:
>
> class Tankung:
> def __init__(self,
> Liter, # set the fuel
> Datum, # set the date
> Preis, # set the price
> Tachostand, # the kilometer count
> Waehrung, # DM or Euro?
> Tankstelle, # where was it bought
> Bemerkung= "", # any remarks?
> prev = None) # the previous one, omit for first in
> chain
> self.bind(locals())
> self.Tachostand = int(Tachostand) # ensure the kilometer count
> is
> # an integer
> try: # build the chained list
> self.prev.next = self
> except TypeError:
> pass # prev was None or alike
>
> def bind(self, keywords):
> for name, value in keywords.items():
> if not (self is value):
> setattr(self, name, value)
>
It looks much like the solution of Wolfgang Grafen, except that you do
a dict.update() by hand and thus avoid having to delete the self
afterwards.
And it has the same problem. It works with Tachostand here (because it
is overwritten anyway) but won't work as soon as you have any parameter
that is only used in calculations, and not assigned to an attribute (or
you need a long exception list in the if in bind()).
markus
--
"The strength of the Constitution lies entirely in the determination of
each citizen to defend it. Only if every single citizen feels duty
bound to do his share in this defense are the constitutional rights
secure." -- Albert Einstein
More information about the Python-list
mailing list