-1

I'm trying to access the methods of the class from which it was instantiated another class, I mean, accessing to the "parent" instance without creating a new instance of it.

class A():  
    def __init__(self):
        ...  
        b_instance = B()  
        ...

class B():
    def __init__(self):  
        ...     
    def function1(self):  
        ...  
    def function2(self):  
        C().run() # I need to use class C functionalities
        ...  

class C():  
    def __init__(self):  
        ...  
    def run(self):  
        classB.function1() #I can't access to these methods without instantiating again class B  


# I have to execute:
>>> a = A()
>>> a.b_instance.function2()

Sorry if I have not explained well, is a bit confusing. If you need any clarification do not hesitate to ask.

EDIT.

In class C a specific handling of the execution of class B methods is done. Is not possible to instanciate again inside C because class B contains the initialization of hardware.

kmsdev
  • 13
  • 3
  • `b_instance` is local to `A.__init__` - it won't be created until you create an instance of `A`, and will not be available outside that method (unless you make it e.g. an instance attribute). Why don't the (presumably) instance methods in `B` and `C` have the `self` parameter? Why does `C.run()` require access to a `B` instance (and, in that case, why isn't one created *in `C.__init__`*)? Could you provide a fuller example that demonstrates what you are trying to achieve in less abstract terms? – jonrsharpe Oct 16 '14 at 10:09
  • Sorry I forgot to put _self_ parameter. Class C requires access to B because it makes specific handling of B methods in threads. I want to maintain isolated in other class for reutilizing from another modules. – kmsdev Oct 16 '14 at 12:24

2 Answers2

0

This can access the class methods from other classes. use instance method, class methods and static methods, if you are using various types of functins.

class A():  
    def __init__(self):
        print 'in __init__'
        self.b_instance = B() # making an instance of class  
        #self.b_instance.function2()

class B():
    def __init__(self):  
        print 'in __init__, B'
    @staticmethod
    def function1():  
        print 'func1'

    def function2(self):  
        C().run() # I need to use class C functionalities

         # if you trying to access `run` method of `class C` make
          # it instance bound method"""

class C():  
    def __init__(self):  
        pass    
    def run(self):
        print 'in run'
        B.function1() #I can't access to these methods without instantiating again class B  
      #you are passing class instance as `B` while calling function1
       # so make it either classmethod `@classmethod` or `static method`

# I have to execute:
a = A()
a.b_instance.function2() # calling b_instance variable of class A
Vishnu Upadhyay
  • 5,043
  • 1
  • 13
  • 24
  • Not work for me because when I define a @staticmethod I have problems to use this method with the class itself – kmsdev Oct 16 '14 at 13:18
  • @kmsdev what problem did you have? – Vishnu Upadhyay Oct 16 '14 at 13:25
  • I lost the access to class variables. Besides I have too many functions and I would need more time to adjust the program than with the solution suggested by jonrsharpe. Thanks anyway for the solution, I sure will be useful sometimes. – kmsdev Oct 16 '14 at 14:02
0

It's still not clear what exactly you're trying to achieve, but here's one fix:

class A():  
    def __init__(self):
        ...  
        b_instance = B()  
        ...

class B():
    def __init__(self):  
        ...     
    def function1(self):  
        ...  
    def function2(self):  
        C().run(self) # pass class B instance to C instance run method
        ...  

class C():  
    def __init__(self):  
        ...  
    def run(self, classB): # note additional parameter
        classB.function1()

However, note that this represents a very high level of coupling between your various classes, which seems suspicious to me and may indicate a deeper flaw in your design.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • This works for me! I thought to solve this would have to do very complicated things... and I forgot the most simple and intuitive way. Just a comment on your solution, I have to put additional parameter in __init__ method also: `def __init__(self, classB) – kmsdev Oct 16 '14 at 13:10
  • Yes, `C(self).run()` works just as well if that's preferable. – jonrsharpe Oct 16 '14 at 13:10