how to create a def that has the behaviour like a built-in keyword...

Niklas Frykholm r2d2 at mao.acc.umu.se
Sun Oct 7 04:05:14 EDT 2001


[Rich Harkins]
>In Python *ALL* functions require parenthesis.  The behaviour you are
>demonstrating would be possible in Perl (and I think Ruby), but Python
>doesn't treat the language syntax and functions as interchangable.  Not
>being able to create functions to masquerade as statements is occasionally
>inconvenient but far more often than not is a good thing IMHO.

Indeed, in Ruby:

	irb(main):003:0> a = [1, 2, 3]
	[1, 2, 3]
	irb(main):004:0> a[0]
	1
	irb(main):005:0> a [0]
	NoMethodError: undefined method `a' for #<Object:0x401e9b14>
			from (irb):5

It does make accessor functions look a bit nicer

	print p.text
	p.text = "OK"

instead of

	print p.getText()
	p.setText("OK")

But I'm not sure whether that is "worth" introducing space-dependent parsing.
All in all, I'm happy with Python being Python and Ruby being Ruby.

// Niklas



More information about the Python-list mailing list