Odp: Getting a class attribute
Lisowski.Tomasz at sssa.nospam.pl
Sat Mar 25 08:56:48 CET 2000
U¿ytkownik Roger Hansen <rogerha at ifi.uio.no> w wiadomooci do grup
dyskusyjnych napisa³:ya0bt44pfdm.fsf at midgard.ifi.uio.no...
> * Oleg Broytmann
> > On 24 Mar 2000, Roger Hansen wrote:
> > > def degree_sin4(self, deg, factor = self.a * math.pi/180.0, sin =
> > > return sin(deg * factor)
> > >
> > >
> > > The question is how can I get "self.a" in degree_sin4?
> > def degree_sin4(self, deg, factor = None, sin = math.sin):
> > if factor is None:
> > factor = self.a * math.pi/180.0
> > return sin(deg * factor)
> Thanks, but it's not what I'm looking for. My question was not precise
> enough, my fault.
> I want to calculate factor as a default parameter value, because I
> want to speed up the method. The method will typically be called
> several thousand times. It is possible to get the class atribute "a"
> in the default function parameter?
Perhaps not, but let's try it the other way. I assume, that you will not
often switch from taking the default value for factor, and setting your own
value for this parameter. If this is true, you could define a class
self.pi_180 = math.pi/180.0
and two methods:
def degree_sin4A(self, deg, factor, sin = math.sin):
return sin(deg * factor)
def degree_sin4B(self, deg, factor, sin = math.sin):
return sin(deg * self.a*self.pi_180)
and then either define
self.mysin4 = self.degree_sin4A
self.mysin4 = self.degree_sin4B
depending on your needs and use self.mysin with the same set of parameters,
as usually. Only with the B version the factor argument will simply not be
used. As the above assignements are only name bindings, you won't lose more
time with calling self.mysin4 than with calling self.degree_sin4A (or B)
directly. Since for arguments the default value is calculated each time, the
function is called (I suppose!), it does not matter, that for version B you
pass an argument, which is not used in the body of the function.
I hope this helps you a little. Comments welcome ...
More information about the Python-list