Reputation: 63
I'm having issues with accessing object attributes when those objects are passed into new classes.
example:
class foo:
def __init__(self, x):
self.x = x
class bar:
def __init__(self, foo: foo, y):
self.foo = foo,
self.y = y
class why:
def __init__(self, bar: bar, z):
self.bar = bar
self.z = z
a = foo(1)
b = bar(a, 2)
c = why(b, 3)
When I try to access b.foo.x
i get an AttributeError: 'tuple' object has no attribute 'x'
but when I try to access c.bar.y
I successfully have 2
returned. Why can I access the y
attribute of the bar
class from the why
class, but I can't access the x
attribute of the foo
class from the bar
class?
I have a series of custom objects that are being passed into custom classes - what is the appropriate way to accomplish this?
There is also a weird case where when I run <parent_object>.<child_object>.__dict__.keys()
I see all of the methods of the <child_object>
, but not of the attributes assigned during the __init__
phase.
Thanks in advance!
Upvotes: 0
Views: 110
Reputation: 915
You have a simple typo — the comment at the end of self.foo = foo,
makes self.foo
a tuple. Remove the comma, and it will work.
Upvotes: 1