Writing "pythonish" code

James Stroud jstroud at mbi.ucla.edu
Fri Feb 2 07:19:11 EST 2007


Mizipzor wrote:
> Hi, this is my first mail to the list (and any list for that matter)
> so any pointers on errors from my part would be appreciated. Im more
> used to forums.
> 
> To start off, Ive read some python documents and done some small apps
> so I think I can say I know it semi-well, and I know c++ very well.
> But I want learn python even better, since I know that a company I aim
> to be employed by make heavy use of python, knowing python myself
> would give me an extra edge.
> 
> The problem isnt in pythons syntax, its in the architecture/design,
> the concept of writing "pythonish code" if you like. One thing is that
> in c++ im used to have private members in classes and no member is
> altered except through the public functions of the class. In python
> everything is, as far as I know, public. Im confused by this, should I
> still have members in the python class that I simply dont edit
> directly and let the class do its internal stuff? Or should I see the
> python classes like c++ structs with functions?
> 
> I guess the ultimate is somewhere in between but I would like a nudge
> or two to get there.
> 
> Now, the thing that bothers me the most. When I write python modules I
> write one class per file, and the file and the class has a common
> name. Maybe this is due to c++ habits. The problem is when I import
> the module, make an instance of its class and store it in a variable:
> 
> foo.py
> =========
> 
> class foo:
>    def bar(self):
>        print "bar"
> 
> =========
> 
> main.py
> =========
> 
> import foo
> 
> localFoo = foo.foo()
> localFoo.bar()
> 
> =========
> 
> To me, the main.py code above looks very ugly. So, assuming Im never
> gonna have more than one instance of the foo class, can I write
> something like this:
> 
> foo.py
> =========
> 
> def bar(self):
>    print "bar"
> 
> =========
> 
> main.py
> =========
> 
> import foo
> 
> foo.bar()
> 
> =========
> 
> Thats much more cleaner if you ask me, and kinda a good way to make
> sure that you dont have more than one "instance" of the foo class
> (which no longer is a class at all). But is it "pythonish"?
> 
> Gonna stop now, this mail got a little longer than i first thought.
> Any input will be greatly appreciated. :)

The "import foo ... foo.bar()" way is preferred. Your instincts are correct.

If you want to write pythonic style, here are some references in roughly 
the order I would read them after the tutorial--this is not a ranking 
but more ordered for study:

    1. google and read the "python style guide"
    2. "How to Think like a Computer Scientist" (google it)
    3. Read this email list and take notes--these people know
    4. David Mertz's "Text Processing in Python" -- a classic (google it)
    5. John Grayson's "Python and Tkinter Programming" (Manning)
    6. "Programming Python" by Mark Lutz (O'Reilly)

I'm sure the cookbook (Ed. Alex Martelli) should be in there, but I 
haven't read it extensively so I don't know where it fits. Probably #7.

James



More information about the Python-list mailing list