Automatic Generation of Python Class Files

Steven Bethard steven.bethard at gmail.com
Mon Oct 22 14:43:05 EDT 2007


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



More information about the Python-list mailing list