*Minor* python usability proposal
Michele Simionato
mis6 at pitt.edu
Thu Feb 13 14:24:16 EST 2003
Eron Lloyd <elloyd at lancaster.lib.pa.us> wrote in message news:<mailman.1045063972.2755.python-list at python.org>...
> Greetings,
>
> As this is my first post to the list, please allow me to apologize ahead of
> time if this subject has been mentioned before. For the record, I _did_
> search the mailing list ahead of time, but didn't find anything.
>
> I've been coding in Python for several years, and find the simplicity and
> straightforwardness of the language almost elegant in style. I'm at the point
> now where I'd like to begin possibly teaching it to kids, as a special
> service here at the library (we're working to develop more high-tech services
> in the computer labs).
>
> One nit I've sort of had was the syntax for declaring functions and methods.
> The keyword "def" seems to me to be anti-intuitive compared to other
> declarative keywords, such as "class" (which is more descriptive). What I
> would like to ask the Python community is whether a more explicit naming of
> "function" for global function definitions and "method" for class method
> definitions would be desirable enough to implement.
>
This is highly unlikely to happen. Whereas I understand that for
children it would be nice to have a visual key to distinguish functions
from methods, the truth is that in Python methods *are* functions (in
the sense Aahz is alluding to). For instance:
>>> class C:
def f(self): pass
print f #here f is a function
gives
<function f at 0x00A9B4F0>
whereas
>>> print C.f # C.f is a method
gives
<unbound method C.f>
The method C.f is nothing else than a wrapping of the original function f;
you see that inside the class f *is* a function, it is only when it is
accessed with the dot notation that it becomes magically a method.
You can also convert functions to methods by hand:
>>> def g(): pass
>>> g
<function g at 0x00AA16F0>
>>> g.__get__(None,C)
<unbound method C.g>
I do not suggest to teach descriptors to children, anyway ;-)
Michele
More information about the Python-list
mailing list