开发者

Python and classes

开发者 https://www.devze.com 2023-01-31 21:54 出处:网络
How i can call first.TQ in开发者_运维知识库 Second ? Without creating object First in Second.

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


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.


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.


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()


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消