Python not that wierd

Martijn Faassen m.faassen at vet.uu.nl
Wed Aug 2 05:17:28 EDT 2000


Steve Lamb <grey at despair.rpglink.com> wrote:
[snip]
>     Yeah, much cleaner that way with the __init__ setting the defaults for the
> class.  st_dice (standard) is 1, 6, 0 respectively.  wod_dice (Worlds of
> Darkness) most likely be 1, 10, 0 respectively.  Rather nice way to do it.  

Note that if you are fine about the defaults being per class instead of
per instance, you can set the defaults per class instead. I.e. the
difference between:

class Foo:
    def __init__(self):
        self.default = "foo"

and

class Bar:
    default = "bar"

The latter now has 'default' shared for all instances. If you do:

self.default = "something"

somewhere in an instance however (like in __init__), that instance will have
the new default. If you want to change the default for the class, you can do:

Bar.default = "something else"

The advantage of using class defaults is that they take up a bit less
memory as they're shared by the instances.

Regards,

Martijn
-- 
History of the 20th Century: WW1, WW2, WW3?
No, WWW -- Could we be going in the right direction?



More information about the Python-list mailing list