Writing "pythonish" code

Mizipzor mizipzor at gmail.com
Fri Feb 2 06:20:46 EST 2007


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. :)



More information about the Python-list mailing list