1
votes

I am trying to create a nested class to perform sum or multiplication of the arguments passed in each subclass. The below example helps me perform action within the class, however I am unable to find any documentation which would help me with inheriting the attributes from the Parent Class to child.

Recently I came across an article which highlights "nested classes can't access any members of their outer classes at compile-time.". Is there a better way to pass the values between Classes? I tried using global variables, but would like to avoid setting many global variables while I scale this logic to extract my entire datacenter's inventory, perform some calculations and again pass to another class.

class Class1:
    firstnumber=0
    def __init__(self,arg):
        self.firstnumber=arg

    class Class2:
        def __init__(self,arg):
            self.secondnumber=arg

        def sumit(self):
            return Class1.firstnumber+Class1.Class2.secondnumber


print(Class1(5).firstnumber)
print(Class1(6).Class2(4).secondnumber)

print(Class1(4).Class2(10).sumit())

I would like to perform calculations with Class1(variable1).Class2(variable2).Class3(variable3).sum() or Class1(variable1).Class2(variable2).Class3(variable3).multiple() and eventually be able to do following

Datacenter('DC1').GetServer('ServerName').GetStorageCapacity('NFS').Used() Datacenter('DC1').GetServer('ServerName').GetStorageCapacity('NFS').Free()

http://momentaryfascinations.com/programming/bound.inner.classes.for.python.html

1
Looks like a misunderstanding on your part. The documentation you cite is correct. The problem you are trying to solve does not really require nested classes. - Mad Physicist
Thanks for constructive feedback. - Atulac

1 Answers

-2
votes

i may be wrong but to my understanding anything you put in between the class() and the init statement is permanent and unchangable. you shouldn't need to create seperate classes for each number. create different instances of the same class.

class numbers:
    def __init__(self,arg):
        self.arg = arg

c1 = numbers(3) c2 = numbers(5)

i don't know how you would add the arg variables together maybe someone else can fill in what i'm missing.