簡體   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