calling class methods from class methods, help?

Discussion in 'Python' started by Oltmans, Mar 11, 2009.

  1. Oltmans

    Oltmans Guest

    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):
    Thread.__init__(self)
    self.times=times
    self.name=''
    def run(self):

    sites=['example.com','example1.com']
    for i in range(0,self.times):
    for site in sites:
    self.name = site
    self.html=SendRequest() # This line throws an error

    def SendRequest(self): #A class method
    # it sends a request to website using mechanize library

    def startThis(times,reqs):

    threads=[]
    for i in range (0,reqs):
    owner=Requests(times)
    owner.start()
    threads.append(owner)

    for thread in threads:
    thread.join()

    if __name__=="__main__":
    #I want to create 2 threads, each of them will execute twice. At
    least that is the intention.
    startThis(2,2)
     
    Oltmans, Mar 11, 2009
    #1
    1. Advertising

  2. Oltmans

    Oltmans Guest

    On Mar 11, 10:08 pm, Oltmans <> wrote:
    >                 self.html=SendRequest() # This line throws an error


    and error says
    NameError: global name '_Requests_SendRequest' is not defined.
     
    Oltmans, Mar 11, 2009
    #2
    1. Advertising

  3. Oltmans

    MRAB Guest

    Oltmans wrote:
    > 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):
    > Thread.__init__(self)
    > self.times=times
    > self.name=''
    > def run(self):
    >
    > sites=['example.com','example1.com']
    > for i in range(0,self.times):
    > for site in sites:
    > self.name = site
    > self.html=SendRequest() # This line throws an error

    Should be:
    self.html = self.SendRequest()

    >
    > def SendRequest(self): #A class method
    > # it sends a request to website using mechanize library
    >
    > def startThis(times,reqs):
    >
    > threads=[]
    > for i in range (0,reqs):
    > owner=Requests(times)
    > owner.start()
    > threads.append(owner)
    >
    > for thread in threads:
    > thread.join()
    >
    > if __name__=="__main__":
    > #I want to create 2 threads, each of them will execute twice. At
    > least that is the intention.
    > startThis(2,2)
    >
     
    MRAB, Mar 11, 2009
    #3
  4. Oltmans

    Chris Rebert Guest

    On Wed, Mar 11, 2009 at 10:08 AM, Oltmans <> wrote:
    > I've a multithreaded program in which I've to call class methods from
    > class methods.


    Um, those are instance methods, not class methods. Class methods take
    the class itself as an argument (the parameter is typically named
    "cls" instead of "self") and are defined with the help of the
    classmethod() function, which is not the case in your code.

    Cheers,
    Chris

    --
    I have a blog:
    http://blog.rebertia.com
     
    Chris Rebert, Mar 11, 2009
    #4
  5. >>>>> Oltmans <> (O) escribió:

    >O> I've a multithreaded program in which I've to call class methods from
    >O> class methods. Here is how my code look like (excluding imports),. Any
    >O> help is highly appreciated.


    >O> #!/usr/bin/env python
    >O> class Requests(Thread):


    >O> def __init__(self, times):
    >O> Thread.__init__(self)
    >O> self.times=times
    >O> self.name=''
    >O> def run(self):


    >O> sites=['example.com','example1.com']
    >O> for i in range(0,self.times):
    >O> for site in sites:
    >O> self.name = site
    >O> self.html=SendRequest() # This line throws an error


    self.html=self.SendRequest()
    --
    Piet van Oostrum <>
    URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
    Private email:
     
    Piet van Oostrum, Mar 11, 2009
    #5
  6. Oltmans

    Oltmans Guest

    On Mar 11, 11:00 pm, Piet van Oostrum <> wrote:
    >                     self.html=self.SendRequest()
    > --


    Thank you, everyone, for the help. Appreciate that.

    > Piet van Oostrum <>
    > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4]
    > Private email: - Hide quoted text -
    >
    > - Show quoted text -
     
    Oltmans, Mar 11, 2009
    #6
  7. Oltmans

    Terry Reedy Guest

    Oltmans wrote:
    > 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):
    > Thread.__init__(self)
    > self.times=times
    > self.name=''
    > def run(self):
    >
    > sites=['example.com','example1.com']
    > 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
    Requests.SendRequest. but...

    > 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):
    >
    > threads=[]
    > for i in range (0,reqs):
    > owner=Requests(times)
    > owner.start()
    > threads.append(owner)
    >
    > for thread in threads:
    > thread.join()
    >
    > if __name__=="__main__":
    > #I want to create 2 threads, each of them will execute twice. At
    > least that is the intention.
    > startThis(2,2)
    >
    >
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Terry Reedy, Mar 11, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Lagemann
    Replies:
    8
    Views:
    496
    Mike Wahler
    Jan 10, 2005
  2. tiwy
    Replies:
    0
    Views:
    448
  3. Oliver Wong
    Replies:
    14
    Views:
    1,634
    Chris Uppal
    Jun 13, 2006
  4. Rhino
    Replies:
    49
    Views:
    1,042
  5. Kenneth McDonald
    Replies:
    5
    Views:
    329
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page