<[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > When I look at how classes are set up in other languages (e.g. C++), I > often observe the following patterns: > 1) for each data member, the class will have an accessor member > function (a Get<whatever> function) > 2) for each data member, the class will have a mutator member function > (a Set<whatver> function) > 3) data members are never referenced directly; they are always > referenced with the accessor and mutator functions > > My questions are: > a) Are the three things above considered pythonic?
As others have said, 'no', as in 'unnecessary because we have a better way to accomplish the same purpose without doubling the attribute namespace'. The purpose of the pattern is to hide the specific implementation of a class (which attributes are static and which dynamic) and to allow that implementation to change without changing programs that use the class. Consider a complex class with interdependent .real, .imag, .rad, and .theta attributes and the possible behind-the-scene implementations for what is kept static and how they are kept synchronized. The need for get/set to accomplish this in C++ arises from the fact that attribute names are resolved at compile time, so that x.a syntax can only be used for simple static attributes and access. Python, on the other hand, has means to 'magically' map what looks like direct attribute access into a function call. First there was __get/setattr__ (which is awkward for multiple dynamic attributes) and now properties with get/set/del for individual dynamic attributes. > b) What are the tradeoffs of using getattr() and setattr() rather than > creating accessor and mutator functions for each data member? > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list