Oct-04-2021, 09:30 PM
(This post was last modified: Oct-04-2021, 09:30 PM by deanhystad.)
You obviously are working with classes, because this kind of thing only works for classes, oh, and everything in Python is an object and is associated with a class.
That you can do it means that the run1 object is an instance of a class that allows setting attributes. This is the default behavior for classes, but many of the built-in classes prohibit doing so. As I mentioned before, you cannot add attributes to list objects or string objects or many built in types because those classes contain code to forbid this.
The question here is not if it is Pythonic or not, because adding attributes to objects is about the most Pythonic thing you can do. The question is, "How much do you trust run1?" What is the class/type of run1? Do you have control over this type? How bad would it be for you if the authors of the code that created run1 decided they want run1 to be a closed class like list or string? How bad would it be for you if the authors of the class decided to add an attribute named "name"?
If I was you, and I had no control over the software that produces run1, I would only do things explicitly stated in the API for that software. I would not use variables that are not documented in the API, even if they are not "protected" using the _name convention. I would not use functions that are not documented in the API. I most certainly would not add my own attributes to objects. If you do those kinds of things you may find the next version of that software breaks your code. And if I decided to give into temptation and stuff a name into run1, I would give it a name that is much less generic than "name".
That you can do it means that the run1 object is an instance of a class that allows setting attributes. This is the default behavior for classes, but many of the built-in classes prohibit doing so. As I mentioned before, you cannot add attributes to list objects or string objects or many built in types because those classes contain code to forbid this.
The question here is not if it is Pythonic or not, because adding attributes to objects is about the most Pythonic thing you can do. The question is, "How much do you trust run1?" What is the class/type of run1? Do you have control over this type? How bad would it be for you if the authors of the code that created run1 decided they want run1 to be a closed class like list or string? How bad would it be for you if the authors of the class decided to add an attribute named "name"?
If I was you, and I had no control over the software that produces run1, I would only do things explicitly stated in the API for that software. I would not use variables that are not documented in the API, even if they are not "protected" using the _name convention. I would not use functions that are not documented in the API. I most certainly would not add my own attributes to objects. If you do those kinds of things you may find the next version of that software breaks your code. And if I decided to give into temptation and stuff a name into run1, I would give it a name that is much less generic than "name".