Automatic Generation of Python Class Files

Sunburned Surveyor sunburned.surveyor at gmail.com
Mon Oct 22 14:45:23 EDT 2007


On Oct 22, 11:43 am, Steven Bethard <steven.beth... at gmail.com> wrote:
> Sunburned Surveyor wrote:
> > 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
> >       """
>
> This should instead generate::
>
> # Inherit from object. There's no reason to create old-style classes.
> class FireBreathingDragon(object):
>
>     # Python is not Java. You don't need getters and setters.
>     # Use public attributes. If you ever decide later that you
>     # need different attributes, you can always use property()
>     # to make your getters and setters look like public attributes
>     def __init__(self, stregth, scariness, endurance):
>         self.strength = strength
>         self.scariness = scariness
>         self.endurance = endurance
>
> STeVe- Hide quoted text -
>
> - Show quoted text -

Thank you for the advice Steve. I didn't know that I should inherit
from Object. I will change my existing Python code accordingly.

Scott Huey




More information about the Python-list mailing list