I have a class GFG with attributes name
, age
and get_city()
.
The method get_city()
receives a string and returns another concatenated string.
>>> class GFG:
... name = 'sdf'
... age = 432
... def get_city(city):
... return f'City: {city}'
...
I tried accessing the attributes name
and age
successfully.
>>> g = GFG()
>>> attr = getattr(g, 'name')
>>> attr
'sdf'
>>> attr = getattr(g, 'age')
>>> attr
432
So I tried to access the method: get_city
as below.
>>> attr = getattr(g, 'get_city')
But when I try to call it by passing a value as an argument I am facing the TypeError
which says it only needs one argument where as I am passing two.
>>> attr('ABCD')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: get_city() takes 1 positional argument but 2 were given
I referred code from this link and followed the pattern where they call the method say_hello
using getattr
feature. Could anyone let me know what is my mistake here?