Use self.vars in class.method(parameters, self.vars)

Chris Torek nospam at torek.net
Fri Jul 22 18:22:27 EDT 2011


In article <0ddc2626-7b99-46ee-9974-87439ae09f1e at e40g2000yqn.googlegroups.com>
caccolangrifata  <caccolangrifata at gmail.com> wrote:
>I'm very very new with python, and I have some experience with java
>programming, so probably you guys will notice.
>Anyway this is my question:
>I'd like to use class scope vars in method parameter ...

Others have answered what appears to have been your actual
question.  Here's an example of using an actual "class scope
variable".

(Note: I have a sinus headache, which is probably the source
of some of the weirder names :-) )

class Florg(object):
    _DEFAULT_IPPY = 17

    @classmethod
    def set_default_ippy(cls, ippy):
        cls._DEFAULT_IPPY = ippy

    def __init__(self, name, ippy = None):
        if ippy is None:
            ippy = self.__class__._DEFAULT_IPPY
        self.name = name
        self.ippy = ippy

    def zormonkle(self):
        print('%s ippy = %s' % (self.name, self.ippy))

def example():
    flist = [Florg('first')]
    flist.append(Florg('second'))
    flist.append(Florg('third', 5))
    Florg.set_default_ippy(-4)
    flist.append(Florg('fourth'))
    flist.append(Florg('fifth', 5))

    for florg in flist:
        florg.zormonkle()

if __name__ == '__main__':
    example()
-- 
In-Real-Life: Chris Torek, Wind River Systems
Intel require I note that my opinions are not those of WRS or Intel
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W)  +1 801 277 2603
email: gmail (figure it out)      http://web.torek.net/torek/index.html



More information about the Python-list mailing list