patternpythonMinor
Class template for beginners
Viewed 0 times
beginnersclassfortemplate
Problem
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:
For the sake of simplicity I don't want to have
This outputs :
Which is exactly what I want, but it looks really weird, obfuscated and non-Pythonic. Any ideas for improvements?
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
42Which is exactly what I want, but it looks really weird, obfuscated and non-Pythonic. Any ideas for improvements?
Solution
This:
is weird. Do this:
it does the same, except comparing classes instead of strings.
This is strange as well:
i is used for iteration so it becomes obsucre, are you calling an int? This:
is more explanatory.
I don't really know about this next part, but you could define a small lambda
with an explanatory name, leaving that part of the code as this:
It leaves it with more words and text but less programming lingo. Might be good.
Now what I would do if this was for someone new to programming is relay on something else then abstraction of the init and abstract in something like biology:
Tested on my significant other :-)
...
while (current_class.__name__ != "Template"):
...is weird. Do this:
...
while current_class != Template:
...it does the same, except comparing classes instead of strings.
This is strange as well:
for i in reversed(inits):
i(self)i is used for iteration so it becomes obsucre, are you calling an int? This:
for init in reversed(inits):
init(self)is more explanatory.
I don't really know about this next part, but you could define a small lambda
extract_first = lambda x: x[0]with an explanatory name, leaving that part of the code as this:
extract_first = lambda x: x[0]
while current_class != Template:
inits.append(current_class.init)
current_class = extract_first(current_class.__bases__)
for init in reversed(inits):
init(self)It leaves it with more words and text but less programming lingo. Might be good.
Now what I would do if this was for someone new to programming is relay on something else then abstraction of the init and abstract in something like biology:
class Exists(object):
perceptible = True
class Alive(Exists):
can_use_sustenance = True
class Vertebrate(Alive):
can_move = True
class Mammal(Vertebrate):
heat_generation = True
class Dog(Mammal):
pass
class Cat(Mammal):
passTested on my significant other :-)
Code Snippets
...
while (current_class.__name__ != "Template"):
......
while current_class != Template:
...for i in reversed(inits):
i(self)for init in reversed(inits):
init(self)extract_first = lambda x: x[0]Context
StackExchange Code Review Q#149289, answer score: 2
Revisions (0)
No revisions yet.