Ryan Paul wrote:
> On Mon, 24 Jan 2005 13:19:45 +0000, Ryan Paul wrote:
>
> >
> > A working solution:
> >
> > class A:
> >   pass
> >
> > a = A()
> > b = A()
> > c = A()
> >
> > [x for x,y in locals().items() if
> >   hasattr(y,"__class__") and y.__class__ == A]
> >
>
> Just wanted to clarify, because I know that the intellectually
deficient
> amongst you will say that isinstance is better than using
__class__...
>
> In this case isinstance isnt desirable because it will catch
> instances of any objects that inherit A, not just instances of A.
Observe:
>
> class A: pass
> class B(A): pass
>
> a = A()
> b = A()
> c = A()
> d = B()
>
> >>> [x for x,y in locals().items() if isinstance(y,A)]
> ['a', 'c', 'b', 'd']
>
> >>> [x for x,y in locals().items() if
> ... hasattr(y,"__class__") and y.__class__ == A]
> ['a', 'c', 'b']

Actually, it this case, isinstance *is* desirable (or rather *would
have been*), exactly for the reason that you point out.  After teaching
about objects, one of the next subject would be that of inheritance.
E.G.

class BetterRobot(Robot):
...  # create a robot that can turn right directly...

However, I have been convinced that it would be much truer to Python to
give the robot fake names (robot1, robot2, ...), and then have students
name their own robots through declarations like:

cleese = Robot(name="Cleese")

or
cleese = Robot()
cleese.name = "Cleese"
=====
André
> 
> -- SegPhault

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to