Automatic Generation of Python Class Files
Marc 'BlackJack' Rintsch
bj_666 at gmx.net
Mon Oct 22 14:23:10 EDT 2007
On Mon, 22 Oct 2007 11:05:52 -0700, Sunburned Surveyor wrote:
> On Oct 22, 10:26 am, "Chris Mellon" <arka... at gmail.com> wrote:
>
> You wrote: " can't think of a single reason why you would ever want to
> do this,
> since your "list of method and property names" would be just as
> verbose as just typing the actual python code."
>
> I don't think I understand how this would be the same amount of
> typing. Consider the following example that would generate a Monster
> class file from an input text file (I only did one property and method
> in generated class file.):
Really a "monster class".
> Contents of input text file:
>
> [Name]
> Fire Breathing Dragon
>
> [Properties]
> Strength
> Scariness
> Endurance
>
> [Methods]
> eatMaiden argMaiden
> fightKnight argKnight
>
> Generated Python Class File:
>
> def class FireBreathingDragon:
>
> def getStrength(self):
> """
> Docstring goes here.
>
> @return
> @rtype
> """
> return self.strength
>
> def setStrength(self, argStrength):
> """
> Docstring goes here.
>
> @param argStrength
> @ptype
> """
> return self.strength
>
> def eatMaiden(self, argMaiden):
> """
> Docstring goes here.
>
> @param argMaiden
> @ptype
> """
Okay this could be written as:
class FireBreathingDragon(object):
def __init__(self, strength):
self.strength = strength
self.scariness = scariness
self.endurance = endurance
def eat_maiden(self, maiden):
pass
def fight_knight(self knight):
pass
Even with all the stuff from your input file it's shorter than your
generated class.
I left out the docs because having no docs is as valuable as your stubs if
not even better. This way you at least see that there is no real docs and
tools like `pylint` can point out the missing docs.
There are editors with good template support for the small amount of
boilerplate that's left.
Just in case you want to defend the default getters and setters: read up
on properties, i.e. the `property()` function, and the discussions in this
group about Python not being Java.
Ciao,
Marc 'BlackJack' Rintsch
More information about the Python-list
mailing list