What are modules really for?
titogarcia_borra_esto at gmail.com
Wed Aug 10 11:55:28 CEST 2005
> Functions existing in a module? Surely if "everything is an object" (OK
> thats Java-talk but supposedly Python will eventually follow this too)
> then there should be nothing in a module thats not part of a class.
Well, all data in a Python program are objects, in the sense that they
have an identity on their own, and variables are only references to them.
Why is it necessary to have all code into classes for meeting the
"everything is an object" panacea?
> a static method is simply a class function that operates on the
> "collection of all instances" rather than a single instance.
That is not true. A static method knows nothing about the instances of a
class, unless you do it your own. Besides, it will work whether you
have created instances of the class or not.
So, a static method is just a global method declared withing a class,
which just serves as a namespace for it.
> Related classes in the same file? Be careful. Doesn't anything "knowing"
> about anything else compromise encapsulation? Why would
> properly-designed classes have such a close relationship?
Question back: why do you think having classes defined in the same file
compromises encapsulation? Classes don't know more about each other for
the fact of being written into the same file. Anyway, in Python, classes
know always too much from each other, don't they?, as there are no
access modifiers for class attributes.
> Having back in the day worked on big real-time systems where being very
> strict about encapsulation was a god-send for fighting complexity, I
> feel unnerved by Perl and Python's laid-back OO culture of "you can do
> it if you feel like it but don't have to".
Well, that is the case with whatever general-purpose programming
language. You have virtually an infinite number of ways to do things,
and most of them are not appropriate.
The thing with Python is, in my opinion, that it wants to put all the
power in your hands to do whatever you want in the fastest way possible.
If I want to do something, I don't declare a class that knows how to do
it, then create it and invoke the right method, I just put the code to
do what I want to do. If, in between, I find that one class would make
my life easier, I declare it just in place and go on. If I find
repetitive tasks I can declare functions, but I won't go for a class
immediately. Why do you think putting code into functions is not
> While you could do all manner
> of nasty hacks in C++ I worked with people who carefully avoided this.
Well done, but messes you can do in whatever language.
More information about the Python-list