Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to access inner class from outer class in python

If possible, not examples with print. But more like with real arguments

Really basic example :

class A:
    def __init__(self):
        pass
    def create_project(self,argA):
        
        newA = call createFromA(argA)
        
        return newA      
        
    class Constructor:
        def __init__(self):
            pass
        def modifA(Abis):
            Abis = Abis + Abis
            return Abis
        def createFromA(self,argA):
            A = 2*argA
            A = modifA(A)
            return A
        
        
cons = A()
cons.create_project(2)

Can someone shed me some light on how I can call createFrom into the outer class

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

createFromA needs to be a staticmethod as it does not operate on instances of the class Constructor

class A:
    def __init__(self):
        pass
    def create_project(self,argA):
        
        newA = self.Constructor.createFromA(argA)
        
        return newA      
        
    class Constructor:
        def __init__(self):
            pass
        @staticmethod
        def createFromA(argA):
            A = 2*argA
            return A
        
        
cons = A()
cons.create_project(2)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading