I would like to understand python classes and objects.
I am fairly new to python and would like to understand in depth.
I am trying and struggling to get the output results from inner-class, Features.
class Person:
def __init__(self):
pass
def first_name(self, first_name ):
self.first_name = first_name
return self.first_name
def middle_name(self, middle_name):
self.middle_name = middle_name
def last_name(self, last_name):
self.last_name = last_name
return self.last_name
class Features:
def __init__(self):
pass
def color(self, color):
self.color = color
return self.color
x = Person()
print(x.last_name('Jimmy'))
print(x.Features.color('Brown'))
Instead I get this Error: TypeError: Person.Features.color() missing 1 required positional argument: ‘color’
Can any of the good souls out there assist kindly. Thanks, and thank you.
I did try some of the stack answers and googled around,
but no satisfying answer.
>Solution :
Your inner class is another class definition, just like the outer one.
a_person = Person()
a_feature = Person.Features()
For using it, you need to define it.
x = Person()
print(x.last_name('Jimmy'))
y = x.Features() # Or just Person.Features()
print(y.color('Brown'))
And the reason for the error:
missing 1 required positional argument
It’s because you’re trying to call the color() function without initializing the Features class. This means that the self argument, which would refer to the initialized class that doesn’t exist now, is not passed down to the color function.
So it’s giving you the error because it takes your "Brown" as the self parameter and so nothing is given to the color parameter; hence the:
Person.Features.color() missing 1 required positional argument: 'color'