What does "all methods in Python are effectively virtual." mean?

all methods in Python are effectively virtual.

While reading Python document I came across this line. What does this mean?

1 Like

Hello @cloud1080432534. I believe it means that you can create a new method with the same name as another, but if it returns a different thing, you can override the original. Take the zip method. If I run the original zip method:


This will print:


You can, however, define a method zip to do something completely different:

def zip(a,b):
  return a+b

I can now call my zip with strings or ints/floats, and it will concatenate or add them:




@thepitycoder, is this explanation correct?

1 Like

I’m stealing this example from (https://inspirated.com/2009/05/03/all-methods-in-python-are-effectively-virtual) because I like it. But if you haven’t done some reading on object-oriented -programming (OOP) yet it’s ok if it doesn’t make sense at the moment.

class Base:
	def hello(self):
		print "Hello called from Base"
	def hello_virtual(self):
		print "Hello called from virtual Base function"
class Derived(Base):
	def hello_virtual(self):
		print "Hello called from virtual Derived function"

d = Derived()


Hello called from Base
Hello called from virtual Derived function

Notice how the self.hello_virtual() triggers the Derived class’ function instead of the Base class (this is the virtual part).

Python… it’s a beaut.

1 Like