Reputation: 13
Hello I really am having a hard time explaining what I want but I can show you to the best ability what I desire programmatically
class C1:
def __init__(self, arg, arg2):
self.arg = arg
self.Foo = arg2
def print_stuff(self):
print(self.arg, self.Foo)
class C2:
def __init__(self, arg):
self.Foo = "bar"
self.C1 = C1(arg, self.Foo)
inst = C2("test")
inst.C1.print_stuff()
I basically want the inst.C1.print_stuff()
to look the same, but I was wondering if there was a better way of implementing.
Here's an incorrect way of writing it just so you can get a better idea of what i'm going for.
# Invalid code below
class C2:
def __init__(self, arg):
self.arg = arg
self.Foo = "Bar"
class C1:
def print_stuff(self):
print(self.arg, self.Foo)
Upvotes: 1
Views: 66
Reputation: 123463
By just doing it:
class C2:
def __init__(self, arg):
self.arg = arg
self.Foo = "Bar"
self.C1 = self.C1(arg, self.Foo)
class C1:
def __init__(self, arg, arg2):
self.arg = arg
self.Foo = arg2
def print_stuff(self):
print(self.arg, self.Foo)
inst = C2("test")
inst.C1.print_stuff() # -> test Bar
inst2 = C2("test2")
inst2.C1.print_stuff() # -> test2 Bar
Upvotes: 1
Reputation: 126
You can create C1
as class C1(C2):
This will create an inheritance link with C1 the subclass of C2
Thus, you can do inst.print_stuff()
directly
Upvotes: 0