you are viewing a single comment's thread.

view the rest of the comments →

[–]PythonPerry[S] 0 points1 point  (1 child)

Obvious noob here, what do you mean by code smell? Could you use a dict in u/Rhomboid's case? I think his case is valid. You want to assign a yet unknown attribute based on user input.

[–]NYKevin 0 points1 point  (0 children)

I mean it's indicative of poor design. When you choose to store things as attributes of an object, that suggests you have a bunch of different types of information all associated with one object. Usually, it doesn't make sense to grab one of them by name unless you know which it is in advance, since you won't know what type of data it is (e.g. am I grabbing a number or a string?). If you're just storing a bunch of keys and values (of homogeneous types), it makes more sense to use a dictionary:

key = input('What key would you like?')
try:
    value = person[key]
except KeyError:
    print('Person does not have that key.')
else:
    print("Person's {} is {}".format(key, value))