简体   繁体   English

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

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

I'm not sure if this is a correct approach of doing it, but i have a class with some functions using the same class on another file. 我不确定这是否是正确的方法,但是我有一个类,其中一些函数在另一个文件上使用相同的类。 The question is, its ok to call a class one time under the init ? 问题是,可以在init下一次叫课吗? what kind of complications could it have if i do it with a class that uses queries/inserts or http requests?. 如果我对使用查询/插入或http请求的类进行操作,会带来什么样的复杂性? Or should i avoid doing it? 还是我应该避免这样做?

Bellow, an example of what i mean: 贝娄,我的意思的例子:

the way i'm doing: 我做的方式:

classfileA.py 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 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()

What i think i can do: 我认为我能做的:

classfileA.py: 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 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()

Thanks in advance. 提前致谢。

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

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

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