简体   繁体   中英

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 ? what kind of complications could it have if i do it with a class that uses queries/inserts or http requests?. Or should i avoid doing it?

Bellow, an example of what i mean:

the way i'm doing:

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

What i think i can do:

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

Thanks in advance.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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