1

How i can call first.TQ in Second ? Without creating object First in Second.

class First:
 def __init__(self):
 self.str = ""
 def TQ(self):
 pass
 def main(self):
 T = Second(self.str) # Called here
class Second():
 def __init__(self):
 list = {u"RANDINT":first.TQ} # List of funcs maybe called in first
 .....
 .....
 return data
asked Dec 24, 2010 at 12:49
2
  • 4
    You can't TQ in Second since Second doesn't have TQ method. You probably want class Second(First):. Also, you shouldn't return a value for __init__. Commented Dec 24, 2010 at 12:52
  • 1
    Want are you exactly trying to achieve? Your example doesn't show an example of Second calling First.TQ. Commented Dec 24, 2010 at 13:09

4 Answers 4

1
class First:
 def __init__(self):
 self.str = ""
 @classmethod
 def TQ(self):
 pass
 def main(self):
 T = Second(self.str) # Called here
class Second():
 def __init__(self):
 list = {u"RANDINT":First.TQ} # List of funcs maybe called in first
 .....
 .....
 return data

Only if you don't intend to call method TQ from First's instance. Happy coding.

answered Dec 24, 2010 at 13:14
Sign up to request clarification or add additional context in comments.

Comments

1

You need to make the TQ-metod static. After a fast checkup on Google (I am not too familiar with Python) I found this: http://code.activestate.com/recipes/52304-static-methods-aka-class-methods-in-python/

It seems to answer your question.

answered Dec 24, 2010 at 13:00

Comments

1

If you want to call a method without creating a object of a class, you need to make a static method of if. Use the staticmethod decorator for this. But keep in mind that static methods do not take the self-parameter, so you can not use self!

class First:
 @staticmethod
 def some_method():
 print 'First.some_method()'
class Second:
 def __init__(self):
 First.some_method()
s = Second()

In the example you posted, you already have an object of First since you are in First.main(). So you can pass the the First's class self to Second:

class First:
 def main(self):
 second = Second(self) # we pass the current object...
 def do_something(self):
 print 'First.do_something()'
class Second:
 def __init__(self, first): # and recieve it as first
 first.do_something()
answered Dec 24, 2010 at 13:14

Comments

1

To clarify some of the answers already posted here, since I don't think any of them really make the point clearly:

Normally, when you define a method on a class in Python, it automatically gets passed as its first parameter the instance on which it is called. If you don't want this to happen, there are two things you can do:

  1. Use the @staticmethod decorator, which will pass no extra argument to the function (that is; it will be called with exactly those arguments with which it looks like it should be called). This is not often useful, since it is commonly clearer just to define a global function and not worry about the class namespace.

  2. Use the @classmethod decorator, which will pass the class on which the function is defined instead of self as the first argument. (This argument is conventionally called cls.) These are often used for functions which apply to the class in general, say from_string or some such.


I wonder why you want to do this; there may be a better way!

answered Dec 24, 2010 at 13:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.