On Feb 14, 10:26 am, Robin Becker <[EMAIL PROTECTED]> wrote: > I'm in the process of porting some code. I have 2.x code that looks like this > > t = type(e) > if t==InstanceType: > return f0(e) > elif t in (float,int): > return f1(e) > else: > return str(e) > > In python 3.0 everything has been unified and people say use attributes to > tell > what should be done in such a branch. However, this is the real world and this > code is fairly complex. Originally we had a distinction between user defined > class instances and those of the builtins like float, int, str etc etc. Is > there > any way for me to tell if an object is an instance of a used defined class > which > can then be tested further perhaps?
Here's something that'll work kind of sometimes. def is_probably_user_defined_class(cls): try: modname = cls.__module__ except AttributeError: return False try: mod = sys.modules[modname] except KeyError: return False try: filename = mod.__file__ except AttributeError: return False return filename.endswith('.pyc') Carl Banks -- http://mail.python.org/mailman/listinfo/python-list