jonuko
jonuko

Reputation: 39

How to instantiate nested class

How can I instantiate a variable of type UseInternalClass?

MyInstance = ParentClass.UseInternalClass(something=ParentClass.InternalClass({1:2}))

If I try the former code, I get an error:

NameError: name 'ParentClass' is not defined

When I want to instantiate an type of a nested class

class ParentClass(object):
    class InternalClass(object):
        def __init__(self, parameter = {}):
            pass
        pass

    class UseInternalClass(object):
        _MyVar

        def __init__(self, something = ParentClass.InternalClass()): #meant to make something type = InternalClass
            _MyVar = something
        pass

(All the code is on the same file)

Upvotes: 1

Views: 4271

Answers (5)

You can instantiate the outer and inner classes with __init__() as shown below:

class OuterClass:
    def __init__(self, arg): # Here
        self.variable = "Outer " + arg
        self.inner = OuterClass.InnerClass(arg)
        
    def outer_method(self, arg):
        print("Outer " + arg)
        
    class InnerClass:
        def __init__(self, arg): # Here
            self.variable = "Inner " + arg
            
        def inner_method(self, arg):
            print("Inner " + arg)

obj = OuterClass("variable") # Here
print(obj.variable)
print(obj.inner.variable)
obj.outer_method("method")
obj.inner.inner_method("method")

Output:

Outer variable
Inner variable
Outer method
Inner method

Upvotes: 0

Arkadiusz Mazur
Arkadiusz Mazur

Reputation: 63

Look for this ona easy example

class Car:

@classmethod
def create_wheel(cls):
    return cls.Wheel()

class Wheel:
    pass

o = Car.create_wheel() print(o) # <main.Car.Wheel object at 0x7fde1d8daeb0>

Upvotes: 0

Nada
Nada

Reputation: 11

I am not sure if i got it right but i if you're trying to do something like that

class Parent:
  class Child:
    def __init__(self,passed_data):
      self.data = passed_data
  class AnotherChild:
    def __init__(self,child=Parent.Child("no data passed"))
      self.child_obj = self.Child(data_to_pass)

you can create AnotherChild object as follows

another_child = Parent.AnotherChild() 
# here it will use the default value of "no data passed"

or you might do it as follows

child = Parent.Child("data") # create child object
another_child = Parent.AnotherChild(child) # pass it to new child

or pass it directly through your init

another_child = Parent.AnotherChild(Parent.Child("data"))

i guess this should work correctly if you are instantiating in the same file for example parent.py , it worked for me like that i am not sure if that what you want but i hope it helps

Upvotes: 0

wigging
wigging

Reputation: 9190

You can do something like this:

class Child:

    def __init__(self, y):
        self.y = y


class Parent:

    def __init__(self, x):
        self.x = x
        y = 2 * x
        self.child = Child(y)

As an example, you create an instance of the Parent class then access its Child as follows:

par = Parent(4)

par.child.y  # returns a value of 8

Upvotes: 1

TwistedSim
TwistedSim

Reputation: 2030

You cannot use "ParentClass" inside the definition of the parent class since the interpreter have not yet define the class object named ParentClass. Also, InternalClass will not be define until the class ParentClass is completly define.

Note: I'm note sure what you are trying to do, but if you explain your end goal, we might be able to suggest you something else to realise that.

Upvotes: 1

Related Questions