At 09:59 AM 8/12/2007, Steve Holden wrote: >Dick Moores wrote: > > At 08:23 AM 8/12/2007, Steve Holden wrote: > >> Dick Moores wrote: > >>> So would a programmer EVER use "is" in a script? > >> Sure. For example, the canonical test for None uses > >> > >> x is None > >> > >> because there is only ever one instance of type Nonetype, so it's the > >> fastest test. Generally speaking you use "is" to test for identity (do > >> these two expressions reference the same object) rather than equality > >> (do these two expressions evaluate to equivalent objects). > > > > Off the top of your head, could you or others give me as many > > examples as you can think of? > > >Occasionally it's necessary to test for a specific type (though in >Python this is usually bad practice). Since types are also singletons >the best way to do this is (e.g.): > > type(x) is type([]) # test specifically for a list > >If you want to know whether you have been told to write to standard >output, one possible test is > > if f is not sys.stdout > >Similarly, of course, you can test for the other standard IO channels. > >The imputil module contains the test > > if importer is not self > >to determine whether a reload() should be performed in the context of >the current package. > >When you need to establish a specific sentinel value that can never be >provided by an outside caller it's normal to create an instance of >object (the simplest possible thing you can create in a Python program) >and test for that instance, as in > > sentinel = object() > ... > if value is sentinel: > >You can test whether a class is new-style as opposed to old-style, which >can help to unify old-style and new-style objects: > >class MetaProperty(type): > def __new__(cls, name, bases, dct): > if bases[0] is object: # allow us to create class Property > return type.__new__(cls, name, bases, dct) > return property(dct.get('get'), dct.get('set'), > dct.get('delete'), dct.get('__doc__')) > > def __init__(cls, name, bases, dct): > if bases[0] is object: > return type.__init__(cls, name, bases, dct) > > >That gets you started ...
Sure does. Thanks very much, Steve. Dick -- http://mail.python.org/mailman/listinfo/python-list