Is there a way to enforce a compile requirement for certain attributes on a class or interface implementation?
For example, let's say my application uses a series of static classes that contain const int
resource values. I'd like to decorate the class in a Description
attribute to describe its contents. In concept, I'd like to apply this attribute requirement to an interface, then each static class would implement it with its required Description
. I could write a run-time check or a unit test to check compliance. But really a compile-time check would be best.
Is there such a thing?
Inside a class, you should qualify all references to class attributes with the class name; for example, MyClass. attr1 . All references to instance attributes should be qualified with the self variable; for example, self.
Adding attributes to a Python class is very straight forward, you just use the '. ' operator after an instance of the class with whatever arbitrary name you want the attribute to be called, followed by its value.
Classes contain characteristics called Attributes. We make a distinction between instance attributes and class attributes. Instance Attributes are unique to each object, (an instance is another name for an object). Here, any Dog object we create will be able to store its name and age.
Accessing the attributes of a classgetattr() − A python method used to access the attribute of a class. hasattr() − A python method used to verify the presence of an attribute in a class. setattr() − A python method used to set an additional attribute in a class.
No, there's nothing like this. Unit tests are the best you can do, as far as I'm aware.
If you can identify the classes automatically (by name, or some other attribute that the class is decorated with) then you can easily write a unit test for all the classes in an assembly.
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