Interesting behaviour of the assignment

Moshe Zadka moshez at zadka.site.co.il
Mon Jan 1 16:58:06 EST 2001


On 01 Jan 2001, Martin von Loewis <loewis at informatik.hu-berlin.de> wrote:

> First I thought: wow, that must be the shortest way to create
> "enumerated" objects. But then I noticed a flaw: SPAM == EGGS.  So I
> still like the FoodType class approach best, although this restriction
> can be worked around with
> 
> SPAM = []
> EGGS = [SPAM]
> WHAT_COMES_NEXT = [EGGS]

This reminds me of the time I needed to create unique objects which are
different from several objects and are immutable. I came up with:

def make_different_from(*args):
	return args

(Oh, yeah, the original objects were immutable too...)

I could have used the old:
class Unique:
	pass

def make_different_from(*args):
	return Unique()

But there were didactical reasons not to.

Consider:

def make_integers_until(n):
	ret = []
	for i in range(n):
		ret.append(apply(make_different_from, ret))
	return ret

Guessing why the function is called that way is an exercise to the reader.

who-needs-numbers-anyway-ly y'rs, Z.
-- 
Moshe Zadka <sig at zadka.site.co.il>
This is a signature anti-virus. 
Please stop the spread of signature viruses!




More information about the Python-list mailing list