[Python-checkins] CVS: python/dist/src/Objects typeobject.c,2.16.8.70,2.16.8.71

Guido van Rossum gvanrossum@users.sourceforge.net
Tue, 31 Jul 2001 00:07:39 -0700


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv21608

Modified Files:
      Tag: descr-branch
	typeobject.c 
Log Message:
Add 'x in type' as a short way to say 'isinstance(x, type)'.
This was proposed on c.l.py and I love it!  (Unfortunately I forget
the name of the person who first proposed it.  He or she deserves a
mention in Misc/ACKS!)


Index: typeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/typeobject.c,v
retrieving revision 2.16.8.70
retrieving revision 2.16.8.71
diff -C2 -d -r2.16.8.70 -r2.16.8.71
*** typeobject.c	2001/07/20 16:26:54	2.16.8.70
--- typeobject.c	2001/07/31 07:07:37	2.16.8.71
***************
*** 783,786 ****
--- 783,806 ----
  };
  
+ static int
+ type_contains(PyObject *type, PyObject *obj)
+ {
+ 	assert(PyType_Check(type));
+ 	return PyType_IsSubtype(obj->ob_type, (PyTypeObject *)type);
+ }
+ 
+ static PySequenceMethods type_as_seq = {
+ 	0,			/* sq_length */
+ 	0,			/* sq_concat */
+ 	0,			/* sq_repeat */
+ 	0,			/* sq_item */
+ 	0,			/* sq_slice */
+ 	0,			/* sq_ass_item */
+ 	0,			/* sq_ass_slice */
+ 	type_contains,		/* sq_contains */
+ 	0,			/* sq_inplace_concat */
+ 	0,			/* sq_inplace_repeat */
+ };
+ 
  static char type_doc[] =
  "type(object) -> the object's type\n"
***************
*** 800,804 ****
  	(reprfunc)type_repr,			/* tp_repr */
  	0,					/* tp_as_number */
! 	0,					/* tp_as_sequence */
  	0,					/* tp_as_mapping */
  	(hashfunc)_Py_HashPointer,		/* tp_hash */
--- 820,824 ----
  	(reprfunc)type_repr,			/* tp_repr */
  	0,					/* tp_as_number */
! 	&type_as_seq,				/* tp_as_sequence */
  	0,					/* tp_as_mapping */
  	(hashfunc)_Py_HashPointer,		/* tp_hash */