Defining accessor methods

Marcin 'Qrczak' Kowalczyk qrczak at knm.org.pl
Mon Jun 25 15:05:53 EDT 2001


Mon, 25 Jun 2001 09:48:23 +0100, Graham Ashton <graham at coms.com> pisze:

>   def my_attribute(self, val=None):

> The obvious problem with the above is that you can't use it to set an
> attribute to None.

The generic solution to such problems is to use the following syntax
instead of default values of parameters:

    def my_attribute(self, *args):

Extra arguments are available in the tuple args. A downside is that you
should implement checking for the right number of arguments yourself,
i.e. somebody might call obj.my_attribute(x,y,z) and it's your business
to detect this.

Arbitrary meaning to keyword arguments is specified thus:

    def my_attribute(self, *args, **kwargs)

Note that it's impossible to detect the order of keyword arguments...

-- 
 __("<  Marcin Kowalczyk * qrczak at knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^                      SYGNATURA ZASTĘPCZA
QRCZAK



More information about the Python-list mailing list