I recently said while trying to use a newer style of classes in Python (those that are derived from an object). As an excersise, to familiarize myself with them, I am trying to define a class that has several class instances as attributes, each of these class instances describing different data types, for example. 1d, 2d arrays, scalars, etc. In fact, I want to write
some_class.data_type.some_variable
where data_type is an instance of a class that describes a set of variables. Below was my first attempt to implement this using only an instance of profiles_1d and fairly common names:
class profiles_1d(object): def __init__(self, x, y1=None, y2=None, y3=None): self.x = x self.y1 = y1 self.y2 = y2 self.y3 = y3 class collection(object): def __init__(self): self._profiles_1d = None def get_profiles(self): return self._profiles_1d def set_profiles(self, x, *args, **kwargs): self._profiles_1d = profiles_1d(x, *args, **kwargs) def del_profiles(self): self._profiles_1d = None profiles1d = property(fget=get_profiles, fset=set_profiles, fdel=del_profiles, doc="One dimensional profiles")
Is the above code an approximately appropriate way to solve this problem. The examples I saw when using property simply set the value of some variable. Here, I need my set method to initialize an instance of some class. If not, any other suggestions on better ways to implement this would be greatly appreciated.
Also, how do I define my set ok method? Typically, the set method, as I understand it, determines what to do when the user types in this example.
collection.profiles1d = ...
The only way I can correctly set the attributes of the profiles_1d instance with the above code is to enter collection.set_profiles([...], y1=[...], ...) , but I think I shouldn't access this method directly. Ideally, I would like to type collection.profiles = ([...], y1=[...], ...) : is this correct / possible?
Finally, I saw decorators mention a new style of activity, but I know very little about it. Is it possible to use decorators here? Is this something I should know more about for this problem?