On Wed, Apr 21, 2010 at 2:15 PM, Tim Shaffer <t...@tim-shaffer.com> wrote:
> On Apr 21, 8:25 am, Torsten Bronger <bron...@physik.rwth-aachen.de>
> wrote:
>> calls the method of the respective class of "instance".  However, in
>> Django,
>>
>> for instance in RootClass.objects.all():
>>     instance.do_something()
>>
>> will *always* call RootClass.do_something(), which is almost never
>> what you want.
>>
>
> I don't necessarily think that's a problem at all. That's the way it
> should work. You want to be able to call RootClass.do_something() on
> an instance of RootClass and have it run that method from one of the
> sub classes?
>

Yes please; this is called 'polymorphism', and is quite a neat
feature. If I were to run:

instance = Derived()
instance.func()

then I would want Derived.func to be executed. If, however, I wrote this:

instance = Derived()
Base.func(instance)

then I would expect Base.func to be executed.

Python doesn't really support 'true' polymorphism anyway, since
instance is always a 'Derived', there is no way to cast or represent
instance as a 'Base', so there is no late-binding (or rather, there is
only late binding).

On the other hand, there is no need, duck typing means there is no
need for objects that are used in exactly the same way being even
being slightly related - take django middleware as an example. They
all implement roughly the same interface, but are not related by type
to any other middleware*.


Cheers

Tom


* OK, so they mostly all derive from object..

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to