3 ms·
Python has it in a more object-oriented way ... def print_string(self): items = ['title', 'header', 'comment'] for item in items:
by bad_user 16y ago
Python has it in a more object-oriented way ...
def print_string(self):
items = ['title', 'header', 'comment']
for item in items:
print getattr(self, item)
Where "getattr" is just a pretty protocol for calling ...
self.__dict__[item]
This extends to whatever you want, including members of namespaces (like declared classes, functions, other namespaces, etc...)
- hagy 16y agoThere is actually a little more to getattr in that __dict__ just has attributes local to that instance where as getattr (also instance.attribute syntax) also accesses class attributes, non-bound methods, and properties. Additionally, these non-local attributes can also be inherited from parent classes.