User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Nested class access

Fri Sep 16, 2016 2:17 pm

In this code is there any way for the 'thisInner' instance to alter 'someData' in 'myOuter'?

Code: Select all

class Inner:
    def changeData(self, newData):
       how to change someData data to newData ??
        
class Outer:
    def __init__(self):
        someData = 1234
        thisInner = Inner()

myOuter = Outer()
myOuter.thisInner.changeData(5678)
The final code will have a number of "outers" each containing a number of different "inners".

Dave.
Apple say... Monkey do !!

User avatar
Paeryn
Posts: 2986
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Nested class access

Fri Sep 16, 2016 4:58 pm

davef21370 wrote:In this code is there any way for the 'thisInner' instance to alter 'someData' in 'myOuter'?

Code: Select all

class Inner:
    def changeData(self, newData):
       how to change someData data to newData ??
        
class Outer:
    def __init__(self):
        someData = 1234
        thisInner = Inner()

myOuter = Outer()
myOuter.thisInner.changeData(5678)
The final code will have a number of "outers" each containing a number of different "inners".

Dave.
As you have it Inner knows nothing about Outer. You could have Inner take an extra parameter when initializing that contains the parent object and have Outer pass itself as that.
Something along the lines of

Code: Select all

class Inner:
    def __init__(self, parent):
        self.outer = parent

    def changeData(self, newData):
       self.outer.someData = newData
        
class Outer:
    def __init__(self):
        self.someData = 1234
        self.thisInner = Inner(self)

myOuter = Outer()
myOuter.thisInner.changeData(5678)
She who travels light — forgot something.

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Nested class access

Tue Sep 20, 2016 11:03 pm

or you could keep a dictionary of instances at the class level

Code: Select all

class Inner(object):
    def changeData(self, newData):
       Outer.InnerOuter[self].someData = newData

class Outer(object):
    InnerOuter = {}
    def __init__(self):
        self.someData = 1234
        self.thisInner = Inner()
        Outer.InnerOuter[self.thisInner] = self

myOuter = Outer()
myOuter.thisInner.changeData(5678)
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Nested class access

Wed Sep 21, 2016 4:53 pm

Paeryn wrote:You could have Inner take an extra parameter when initializing that contains the parent object and have Outer pass itself as that.
I went with that and it worked without over complicating things (the end product is aimed at kids so trying to keep it simple, didn't want to start with parent/child etc.). I should have probably mentioned that the Inner and Outer classes would be in separate files but in this case it didn't matter.

Thanks.
Dave.
Apple say... Monkey do !!

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Nested class access

Wed Sep 21, 2016 6:40 pm

I agree, just having the parent ref in the child is the most straightforward solution. Occasionally there can be circular references that stop the garbage collector tidying up properly and lead to memory leaks so in those (rare) cases the dictionary system might provide a fix.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”