[Python-Dev] __contains__ hook

Moshe Zadka Moshe Zadka <mzadka@geocities.com>
Tue, 1 Feb 2000 21:59:27 +0200 (IST)


  This message is in MIME format.  The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.
  Send mail to mime@docserver.cac.washington.edu for more info.

---559023410-851401618-949435167=:17780
Content-Type: TEXT/PLAIN; charset=US-ASCII


Here's a very preliminary, very hackish version of a hook for the "in"
operator. 

Basically, use like:

class spam:
	def __contains__(self, o):
		return 1
6 in spam() (answers 1)

I must say I was horrified by the current way the operator was handled:
very non-OO-ish. I'd much rather there'd be a slot in the sequence
interface for this method. This is why there's still no way to use the
hook with regular C extension types.

Have fun!

(BTW: I've tested it only minimally, so it might break your Python. Use
with caution).

PS.
Eric, you can implement sets the *right* way this time.

--
Moshe Zadka <mzadka@geocities.com>. 
INTERNET: Learn what you know.
Share what you don't.

---559023410-851401618-949435167=:17780
Content-Type: TEXT/PLAIN; charset=US-ASCII; name=1
Content-ID: <Pine.SOL.3.96.1000201215927.17780E@sundial>
Content-Description: 
Content-Transfer-Encoding: BASE64

KioqIC4uLy4uLy4uL3B5dGhvbi9kaXN0L3NyYy9PYmplY3RzL2Fic3RyYWN0
LmMJRnJpIE9jdCAxNSAxNDowOTowMiAxOTk5DQotLS0gT2JqZWN0cy9hYnN0
cmFjdC5jCVR1ZSBGZWIgIDEgMTA6MzQ6MzQgMjAwMA0KKioqKioqKioqKioq
KioqDQoqKiogMTExMCwxMTE1ICoqKioNCi0tLSAxMTEwLDExNDAgLS0tLQ0K
ICAJCX0NCiAgCQlyZXR1cm4gMDsNCiAgCX0NCisgCS8qIHNwZWNpYWwgY2Fz
ZSBmb3IgaW5zdGFuY2VzLiBCYXNpY2FsbHkgZW11bGF0aW5nIFB5dGhvbiBj
b2RlLA0KKyAJICAgYnV0IG9wdGltaXphdGlvbnMgd2lsbCBjb21lIGxhdGVy
ICovDQorIAlpZiAoUHlJbnN0YW5jZV9DaGVjayh3KSkgew0KKyAJCVB5T2J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