Getting a class attribute

Alex alex at somewhere.round.here
Fri Mar 24 10:32:45 EST 2000


> It is possible to get the class atribute "a" in the default function
> parameter?

My understanding is that the default values are computed at compile
time, so not really.  Perhaps you could redefine the function each time
the member 'a' changes?  Here is an example.  It does not really make
new_degree_sin4 into a class method, as I don't know how.  I am pretty
sure it's possible and desirable to do so, though.  Hopefully someone
else will tell you.

Come-join-my-cargo-cult'ly yrs
Alex.

import math

class Eg:

    def __init__ (self):
        self.set_factor (1)

    def set_factor (self, a):
        self.a = a
        def new_degree_sin4 (deg, factor=(a*(math.pi/180)),
                             sin=math.sin):
            return sin (deg * factor)
        self.new_degree_sin4 = new_degree_sin4

if __name__ == '__main__':
    eg = Eg ()
    print eg.new_degree_sin4 (5)
    eg.set_factor (6)
    print eg.new_degree_sin4 (5)

    



More information about the Python-list mailing list