thclpr
thclpr

Reputation: 5938

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.

Upvotes: 0

Views: 104

Answers (1)

Maciej Gol
Maciej Gol

Reputation: 15864

Yes, it's fine in sense that nothing will blow up in regards to the someClass.StackExample object and the calling object.

Upvotes: 1

Related Questions