Need to call class method from different class without initialization of the first class or some other way around it Need to call class method from different class without initialization of the first class or some other way around it tkinter tkinter

Need to call class method from different class without initialization of the first class or some other way around it


In general you can call a class method from anywhere you want and pass anything to it without initialisation of that class's instance, thanks to objective nature of python, but beware of self dependencies! Although, I don't think that's a good practice.

class A:    def __init__(self):        self.foo = 'foo'    def return_foo(self):        return self.fooclass B:    def __init__(self):        self.bar = 'bar'        print('Ha-ha Im inited!')    def return_bar(self):        try:            return self.bar        except AttributeError:            return 'bar'def test():    a = A()    # b = B()    return_bar = getattr(B, 'return_bar', None)    if callable(return_bar):        print('%s%s' % (a.return_foo(), return_bar(None)))test()

Links: