Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have code that goes like this:

class A(object):  
    def __init__(self, master):  
        """Some work here"""  

    def do_this(self):  
        self.B = B.do_that()  
        print self.B[1]  


class B(object):  
    def __init__(self, master):  
        """Some work here"""  

    def do_that(self):  
        p = (1, 2)  

I can't make the method in class A to use that self.B as a tuple. Help.

share|improve this question

2 Answers

up vote 1 down vote accepted

For starters, do_that() doesn't return anything. So calling it will pretty much do nothing.

self.B = B.do_that() also won't work. You have to first create an instance of the class B:

mything = B(your_parameters)
mything.do_that()

And if you want that to return something (i.e, the tuple), you should change your do_that() to:

def do_that(self):  
    return (1, 2)

One final note, this can all be achieved through Inheritance:

class A(B): # Inherits Class B
    def __init__(self,master):
        """Some work here""" 
    def do_this(self):
        print self.do_that()[1] # This is assuming the do_that() function returns that tuple

Using the inheritance method:

>>> class B:
...     def __init__(self, master):
...         """Some work here"""
...     def do_that(self):
...         return (1,2)
... 
>>> class A(B):
...     def __init__(self, master):
...         """Some work here"""
...     def do_this(self):
...         print self.do_that()[1] 
...
>>> mything = A('placeholder')
>>> mything.do_this()
2
share|improve this answer
yeah okay. thank you. I wont use inheritance though, because I have to inherent from frame/object for this assignment. – user2372332 yesterday
@user2372332 Ah okay, so probably the other suggestions will help you :) – Haidro yesterday

First you have to instantiate B as a property of A in the method A.do_this(). The following code should work.

def do_this(self):
    b = B()
    self.B = b.do_that()  
    print self.B[1]  
share|improve this answer
thank you for your response – user2372332 yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.