Gipphe
Gipphe

Reputation: 179

Python: class methods calling other class methods when initial method is called from within another class

I am working on an elaborate file switcher (in lack of a better term), and have run into a problem. Simplified, consider the code below:

class Foo(object):
    def __init__(self):
        self.foo = Bar()
        self.foo.baz()

class Bar(object):
    def baz(self):
        print("baz")
        self.qux()
    def qux(self):
        print("qux")

When the class Foo initiates, and calls "self.foo.baz()", nothing is printed in the output whatsoever. How come? Is there a way to call methods within the Bar class from within Foo properly in this case?

Upvotes: 1

Views: 899

Answers (1)

mgilson
mgilson

Reputation: 309919

__init__ isn't called until you create an instance. If you create an instance:

a = Foo()

Then you should see something printed.

Upvotes: 2

Related Questions