creating class objects inside methods

Albert Hopkins marduk at letterboxes.org
Sun Oct 4 12:12:04 CEST 2009


Just by a brief look at your code snippet there are a few things that I
would point out, stylistically, that you may consider changing in your
code as they are generally not considered "pythonic":

      * As already mentioned the "state" class is best if given a name
        that is capitalized.
      * As already mentioned the name "state" is (unintentionally)
        re-used within the same scope.  This could have been avoided
        with the above naming convention change and also if the main
        body of the code were in a function, say "main()"
      * The "methods" default_board, default_types, and default_moves
        appear to be static methods but are not defined as such.
      * Also, since the above "methods" have no logic and simply return
        a value they are probably best defined as static attributes
        rather than methods. But if doing so you would need to alter
        your constructor.
      * You used a number of built-in names as attribute names (dir,
        type). While this is legal Python it is generally not
        recommended.
      * You use range(0, n).  0 is the default start value for range()
        so it is generally left out.
      * You define a to_string() method. To have a string representation
        of a class, one usually defines a __str__ method.  This gives
        the advantage whereby "print myobject" or '%s' % myjobject just
        work.

Probably others can make more suggestions. You will find a lot of these
suggestions are borrowed from PEP8 [1] but you can peruse other people's
Python code to "learn from the masters".

Hope this helps,
-a





More information about the Python-list mailing list