Idea about method parameters

Duncan Booth duncan at NOSPAMrcp.co.uk
Fri Sep 28 04:48:34 EDT 2001


Wolfgang Grafen <wolfgang.grafen at gmx.de> wrote in 
news:3BB3A315.5B49A0FB at gmx.de:

> Attempt 3:
> 
>>>> class A:
> ...     def __init__(self,a,b,c=333,d=444):
> ...         e = d + c
> ...         del c,d
> ...         self.__dict__.update(vars())  # save a,b,e
> ...         del self.__dict__['self']     # remove cyclic self reference
> ...     def __call__(self):
> ...         print self.a,self.b,self.e
> ...
>>>> a=A(1,2)
>>>> a()
> 1 2 777

If you want to initialise the instance variables while still keeping c,d 
around then you can get rid of that first del by pushing the 
self.__dict__.update() out into another function:

from __future__ import nested_scopes

class A:
    def __init__(self,a,b,c=333,d=444):
        def _init_instance(a, b, e):
            """Copy all parameters to instance variables"""
            self.__dict__.update(locals())
            del self.self   # remove cyclic self reference

        e = d + c
        _init_instance(a, b, e)

    def __call__(self):
        print self.a,self.b,self.e
        print self.__dict__
a=A(1,2)
a()


-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list