*Minor* python usability proposal

Hans Nowak wurmy at earthlink.net
Wed Feb 12 14:29:36 EST 2003


Eron Lloyd wrote:

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

A change like this will never be accepted, because it will break just about 
every Python file on the planet.  Of course, you can grab the soure and (find 
somebody to) hack it so it will do what you want, but this will probably be 
non-trivial. :-)

> I'm not a core developer, and cannot actually modify Python's internals to 
> support this. I do think it would be in keeping with Python's "easy to learn" 
> reputation that when I explain to the children about defining a method vs. a 
> function, there will be some visual acuity to this logical difference between 
> the two. I imagine this could be done without breaking old code...

The difference between functions and methods is not as great as you might think:

# a class with a method
 >>> class Foo:
	def hello(self, name):
		print "Hello, %s!" % (name,)

# instantiate the class	
 >>> foo = Foo()

# call the method like a function
 >>> hiya = foo.hello
 >>> hiya("Fred")
Hello, Fred!

# and again
 >>> hi = Foo.hello
 >>> hi(foo, "John")
Hello, John!

# define a function
 >>> def bye(self): print "Ta-ta!"

# stick in into Foo so it magically becomes a method
 >>> Foo.bye = bye
 >>> foo.bye()
Ta-ta!

Cheers,

-- 
Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA=='))
# decode for email address ;-)
The Pythonic Quarter:: http://www.awaretek.com/nowak/
Kaa:: http://www.awaretek.com/nowak/kaa.html
Soon: http://zephyrfalcon.org/





More information about the Python-list mailing list