Python misconceptions in IBM Ruby article...

Thomas Wouters thomas at xs4all.net
Fri Feb 18 12:32:18 EST 2000


On Fri, Feb 18, 2000 at 09:33:12AM +0000, Robin Becker wrote:
> I guess the objection is to not having a standard name for the instance
> in class functions. If we had the standard set at self you could
> presumably write

> class dingo:
>     def normal_func(a1,a2,....):
>         .....

>     classdef instance_func(a1,a2,...):
>         self.a=1
>         ........

> ie you could distinguish in the class definition between the kinds of
> functions. Presumably normal_func cannot refer to self and instance_func
> can. Also I assume that normal_func would live while the class does
> which is more difficult in python.

You could possibly do it that way, but Guido didn't ;) I personally like
this very, very much (combined with all other 'naming' conventions and rules
in Python) -- it means something very simple and intuitive:

Python does not introduce (at runtime) *any* names in the local or global
namespace, that you have not explicitly asked for (this is one of the two
reasons to avoid an exec without local/global dicts, and 'from foo import*')

This is really nice if you're faced with a long, hairy (un-Pythonic)
function with an, uhm, interesting mix of locals and gobals. Bare exec's and
'*'-imports are, thankfully and rightfully, pretty rare, making it nice and
easy to trace back, manually, where a variable came from and where it is
going (in absence of continuations, of course <duck> ;)

(There are a lot of bindings in the global namespace that python does
introduce, namely all the builtin functions, but those are always there.
They do not depend on some other, at first glance unrelated, statement or
function call.)

So, you have to type five characters more, per (local) attribute. As a
bonus, you wont be so annoyed when you see you have a global variable
'feebles' and a class/instance attribute 'feeble', and you typo the name ;)


-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list