繁体   English   中英

在__init__内调用类/函数是正确的吗?

[英]It's correct to call a class/function inside __init__?

我不确定这是否是正确的方法,但是我有一个类,其中一些函数在另一个文件上使用相同的类。 问题是,可以在init下一次叫课吗? 如果我对使用查询/插入或http请求的类进行操作,会带来什么样的复杂性? 还是我应该避免这样做?

贝娄,我的意思的例子:

我做的方式:

classfileA.py

class StackExample:

    def __init__(self, input_a, input_b)
        self.var_a = input_a
        self.var_b = input_b

    def say_hello()
        print "My bacon is : " + self.var_a
        print "But still boss as : " + self.var_b

-

file2.py

import classfileA

class Action:

    def __init__(self, input_a, input_b)
        self.var_a = input_a
        self.var_b = input_b    

    def test_a()
        start = classfileA.StackExample(self.var_a,self.var_b)
        start()

    def test_b()
        start = classfileA.StackExample(self.var_a,self.var_b)
        start()

    def test_c()
        start = classfileA.StackExample(self.var_a,self.var_b)
        start()

if __name__ == '__main__':
    run = action('my love','blind')
    run_cm.main()

我认为我能做的:

classfileA.py:

class StackExample:

    def __init__(self, input_a, input_b)
        self.var_a = input_a
        self.var_b = input_b

    def say_hello()
        print "My bacon is : " + self.var_a
        print "But still boss as : " + self.var_b

-

file2.py

import classfileA

class Action:

    def __init__(self, input_a, input_b)
        self.var_a = input_a
        self.var_b = input_b
        # call inside __init__
        self.start = classFileA.StackExample(self.var_a,self.var_b)


    def test_a()        
        self.start()

    def test_b()
        self.start()

    def test_c()
        self.start()


if __name__ == '__main__':
    run = Action('my love','blind')
    run.test_a()

提前致谢。

是的,从某种意义上来说,关于someClass.StackExample对象和调用对象不会有任何问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM