Re: Dynamically adding a runtime generated method to a class.
- From: "Martin v. Löwis" <martin@xxxxxxxxxxx>
- Date: Fri, 30 Nov 2007 08:26:52 +0100
This would work and not be too terribly inefficient but I was thinking
it would be much better generate the getLine method at runtime and
dynamically add it to the class so I wouldn't have to pass the columns
around and loop through them every time;
I would advise against that. Generating methods on the fly is fairly
involved (although possible); but I see no gain.
If you don't want to pass the cols around (which I can see as
desirable), I recommend to make them an instance variable:
def getLine(self, per):
vals = [self.desc]
for col in self.cols:
vals.append(col(self, per))
return vals
line1 = lineClass1('Some Description', [1,2,3,4,5,6,7,8,9,10,11,12])
line1.cols = [lineClass1.Ptd, lineClass1.Ytd]
print line1.getLine(5)
HTH,
Martin
.
- References:
- Dynamically adding a runtime generated method to a class.
- From: Joe Goldthwaite
- Dynamically adding a runtime generated method to a class.
- Prev by Date: Re: Oh no, my code is being published ... help!
- Next by Date: Re: Dynamically adding a runtime generated method to a class.
- Previous by thread: Dynamically adding a runtime generated method to a class.
- Next by thread: Re: Dynamically adding a runtime generated method to a class.
- Index(es):