What is a good practice to model Python properties in a UML class diagram? Properties themselves are class objects, their getter and setter are class functions. From Outside the class they look like instance attributes. So, how would you suggest to present that in my class diagram?
I recommend you read UML Best Practice: Attribute or Association, by another Stack Overflow user named Geert Bellekens. It states simply:
Use Associations for Classes and Attributes for DataTypes.
You should write the Python attributes that are typed by non-datatypes (which have identity) at the ends of UML associations connecting to those UML classes. You should write the Python attributes typed by simple datatypes (which have no identity other than their value) in the UML class' attribute box.
The accessors and mutators are largely just noise. A model compiler or IDE can generate those for you.
Good practice is what works on your project.
In order to model Python properties you can add stereotyped getter and setter operations which indicate their use. The link between attribute and operation is usually done via a naming convention. Some tools offer internal linkage to make attributes properties with getters and setters.
If you are not using code generation you can also stereotype the attribute to indicate their use as properties (thus telling the coder to use @property
) and leave away the operations. If you are using your own code generator this would work analogously. Tool embedded code generators might need the additional operations as described above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With