Idea about method parameters

Kirill Simonov kirill at xyz.donetsk.ua
Wed Sep 26 14:22:44 EDT 2001


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.

>                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


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)


Kirill





More information about the Python-list mailing list