Answer the question
In order to leave comments, you need to log in
"Why doesn't {this code} work?" or “How do I create a method that will create any method?” in Python?
Hello colleagues.
The question is not practical, just curious.
Here I was trying to turn the argument into an attribute.
Why is this code not working? ↓
class ClassName:
def add(self, **kwargs):
for key, value in kwargs.items():
eval(f'self.{key} = {value}')
exemplar = ClassName()
exemplar.add(name = 'Python')
print(exemplar.name)
# File "<string>", line 1
# self.name = Python
# ^
# SyntaxError: invalid syntax
class ClassName:
def add(self, **kwargs):
for key, value in kwargs.items():
eval(f'self.{key} = "{value}"')
exemplar = ClassName()
exemplar.add(name = 'Python')
print(exemplar.name)
# File "<string>", line 1
# self.name = "Python"
# ^
# SyntaxError: invalid syntax
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question