0

How do I call method .get_item(Example1()).do_something() in class Base from inside the class Example2?

class Base:

    items = []

    def add_item(self, item):
        self.items.append(item)

    def get_item(self, item):
        return self.items[0]

class Item:
    pass

class Example1(Item):

    def do_something(self):
        print('do_something()')

class Example2(Item):

    def __init__(self):
        '''
        How call this method .get_item(Example1()).do_something() ?
        '''

if __name__ == '__main__':

    base = Base()
    base.add_item(Example1())
    base.add_item(Example2())
3
  • 2
    Are you trying to call another method in the __init__ of Example2 or are you trying to call the __init__ method itself? Actually I can't understand what you are asking. Please provide more information. Commented Sep 9, 2011 at 9:08
  • I wan't from Example1() or Example2() have access to methods from Base() class. Commented Sep 9, 2011 at 9:12
  • @vladislav godorin With two "from" in it, your sentence means nothing. Don't you read what you writed before posting ? Commented Sep 9, 2011 at 9:24

2 Answers 2

2

If you want such functionality, you need to pass Base to Example2 guy:

class Example2(Item):

    def __init__(self, base):
         self.base = base
         self.base.get_item(Example1()).do_something()

         # or if don't want to store base, and just call its method once:
         base.get_item(Example1()).do_something()

if __name__ == '__main__':

    base = Base()
    base.add_item(Example1())
    base.add_item(Example2(), base)
Sign up to request clarification or add additional context in comments.

Comments

0
self.get_item(...)

or

Base.get_item(self, ...)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.