I am going to try my best to explain what I am trying to accomplish. I am trying to return an instance of a new class from a class instead of return self. Please refer to the comments in the example code.
class Test(object):
def __init__(self, a):
self.a = a
def methoda(self):
return len(self.a)
class SomeClass(object):
def __init__(self, lol):
self.lol = lol
self.test = Test(self.lol)
def __call__(self):
return self.test # this isnt going to work
c = SomeClass('bar')
print(c) # trying to access the Test class attributes and methods here
# so in the above example, i want to do
# print(c.a) # print bar
# print(c.length() # print 3
__repr__ and __str__ wouldnt work in this case because I am trying to get the Test class object back.
Other things i have tried in SomeClass is having something like self.test = Test(self.lol), but that doesnt seem to quite do what I want.
How can I do this?
print(c())? You're having the object act as a function, so you'll need to call it.c.test. You already have it as an attribute.