I want to allow anyone that has never coded to make classes depending on my template with little to no knowledge of Python.
These classes need to do 2 things:
- Inherit from other classes similar to them.
- Contain variables, updating values if they have been changed or keeping them if not.
For the sake of simplicity I don't want to have super()
on these new classes, as I'm trying to not frighten newcomers with things they can't understand yet. Here's how I did it:
class Template(object):
def __init__(self):
self.value = "hello"
self.other_value = "bonjour"
self.constant_value = 42
current_class = self.__class__
inits = []
while (current_class.__name__ != "Template"):
inits.append(current_class.init)
current_class = current_class.__bases__[0]
for i in reversed(inits):
i(self)
def init(self):
pass
def info(self):
print self.value
print self.other_value
print self.constant_value
print ""
class Deep(Template):
def init(self):
self.value = "howdy"
self.other_value = "salut"
class Deeep(Deep):
def init(self):
self.value = "hi"
class Deeeep(Deeep):
def init(self):
self.value = "'sup"
very_deep = Deeeep()
not_so_deep = Deep()
very_deep.info()
not_so_deep.info()
This outputs :
sup salut 42 howdy salut 42
Which is exactly what I want, but it looks really weird, obfuscated and non-Pythonic. Any ideas for improvements?
Template
class? Should they create their own classes which inherit fromTemplate
, or should they simply make a copy of the code and modify it to their needs? In either case I don't understand how this is going to be easier for them than just learning to actually write their own class which does what they need. \$\endgroup\$'sup
, notsup
. That has mistakenly been changed in rev. 2, but I can't fix it because it's only a single-character edit. \$\endgroup\$