While everyone is saying what they want in Python :)

Daniel Klein danielk at aracnet.com
Sun Feb 4 11:20:24 EST 2001


On Sun, 04 Feb 2001 14:19:04 +0000, Jay O'Connor <joconnor at cybermesa.com>
wrote:

>Smalltalk-style cascade operations would be *very* cool
>
>win = GtkWindow();
>	set_title("Hello World");
>	set_name ("window");
>	set_usize (400,200)
>
>versus
>
>win = GtkWindow()
>win.set_title("Hello World")
>win.set_name ("window")
>win.set_usize (400,200)


That's cos Smalltalk returns 'self' by default when a there is no explicit
return value. This can be done in Python if your 'set' methods return 'self'
instead of 'None'. For example:

>>> class A:
	def m1(self, t1):
		self.t1 = t1
		return self
	def m2(self, t2):
		self.t2 = t2
		return self
	def m3(self, h, w):
		self.h = h
		self.w = w
		return self
	
>>> a = A().m1("Hello World").m2("window").m3(400,200)
>>> a.h
400

pythonic-ly yr's,

Daniel Klein
Portland OR USA




More information about the Python-list mailing list