0

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?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Metadata
  • 2,127
  • 9
  • 56
  • 127
  • It's not really your mistake. The code in the link is wrong, and doesn't match the output they're showing. – wjandrea Nov 15 '22 at 18:05

0 Answers0