Idea about method parameters
Markus Schaber
markus at schabi.de
Tue Sep 25 05:31:18 EDT 2001
Erik Max Francis <max at alcyone.com> schrub:
> Markus Schaber wrote:
>
>> Now I'd love to have the possibility to shorten this by typing:
>>
>> class A:
>> def m(self, self.value):
>> pass # or the other work to be done
>>
>> This should do exactly the same as:
>>
>> class A:
>> def m(self, value):
>> self.value = value
>> del value
>> #the other work here
>
> Explicit is better than implicit. Saving a few keystrokes shouldn't
> be sufficient reason to add such an irregular exception.
I would not call it an exception only because common languages don't
have it already.
With def, you tell the interpreter to bind the values the caller gives
to some method-local names. I just give the ability to bind this values
to names from the objects namespace as well.
Well - compare the following example just reconstructed from
a class I use in a small database, building a chained list:
class Tankung:
def __init__(Liter, # the amount of fuel
Datum, # the date
Preis, # the price
Tachostand, # the kilometer count
Waehrung = "DM", # DM or Euro?
Tankstelle, # where was it bought
Bemerkung= "", # any remarks?
prev = None) # the previous one, omit for first in chain
self.Liter = Liter # set the amount of fuel
self.Datum = Datum # set the date
self.Preis = Preis # set the price
self.Waehrung = Waehrung # set the currency
self.Tankstelle = Tankstelle # set the fuel station
self.Bemerkung = Bemerkung # set the remarks
self.Tachostand = int(Tachostand) # ensure the kilometer count is
# an integer
self.prev = prev # set the previous one
try: # build the chained list
self.prev.next = self
except TypeError:
pass #prev was None or alike
and in the proposed syntax:
class Tankung:
def __init__(self.Liter, # set the fuel
self.Datum, # set the date
self.Preis, # set the price
Tachostand, # the kilometer count
self.Waehrung = "DM", # DM or Euro?
self.Tankstelle, # where was it bought
self.Bemerkung= "", # any remarks?
self.prev = None) # the previous one, omit for first in
# chain
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
I just rebuilt it from brain because the source is on another machine,
and put the Comments in English instead of German so there might be
typos :-)
In my eyes, the second example is more clear and elegant.
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