calling class methods from class methods, help?
tjreedy at udel.edu
Wed Mar 11 20:59:25 CET 2009
> I've a multithreaded program in which I've to call class methods from
> class methods. Here is how my code look like (excluding imports),. Any
> help is highly appreciated.
> #!/usr/bin/env python
> class Requests(Thread):
> def __init__(self, times):
> def run(self):
> for i in range(0,self.times):
> for site in sites:
> self.name = site
> self.html=SendRequest() # This line throws an error
You should (almost) always display the error traceback. I suspect
NameError: global 'SendRequest' not found. You need
> def SendRequest(self): #A class method
If it were, then call the parameter 'cls', not 'self'. But it is not a
classmethod without @classmethod decorator. but...
> # it sends a request to website using mechanize library
Does this need to send the class rather than instance object to the
website? If not, better to leave it an instance method and use
self.SendRequest above. If the request uses instance variables, then it
*must* be an instance method!
> def startThis(times,reqs):
> for i in range (0,reqs):
> for thread in threads:
> if __name__=="__main__":
> #I want to create 2 threads, each of them will execute twice. At
> least that is the intention.
More information about the Python-list