[correction]an oop question

Alan Gauld learn2program at gmail.com
Wed Nov 2 20:15:05 EDT 2022


On 02/11/2022 20:21, Dennis Lee Bieber wrote:

>>  shows that in Python we do *not* need subclassing/inheritance
>>  for polymorphism!
>>
> 	To me, that is not really an example of polymorphism, but more an
> example of Python's "duck typing".

But duck typing is a perfectly good implementation of polymorphism.
The term just means that different objects respond to the same
message in different ways. Nothing more, nothing less. Inheritance
just happens to be the most common way of building that, especially
in statically typed languages.

> 
> 	I'd implement the example hierarchy as
> 
>>>> class Language:
> ... 	def f(self):
> ... 		print(self.greeting)

And that would be perfectly valid too.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Python-list mailing list