Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8884743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:08:11+00:00 2026-06-14T21:08:11+00:00

I am using Python 2.7 (google app engine) Learning Python , I have figured

  • 0

I am using Python 2.7 (google app engine)

Learning Python, I have figured out how the language uses inheritance to achieve polymorphism.

INHERITANCE-BASED POLYMORPHISM IN PYTHON:

class pet:
  number_of_legs = None
  age = None

  def sleep(self):
    print "zzz"

  def count_legs(self):
    print "I have %s legs." % self.number_of_legs
    return self.number_of_legs


class dog(pet):
  def bark(self):
    print "woof"

doug = dog()

doug.number_of_legs = 5;

print "Doug has %s legs." % doug.count_legs()
doug.bark()

My question is this: Does Python have loosely coupled polymorphism, i.e. using interface instead of inheritance. For example, in Java, classes with no ancestral relationships may implement the same interface. See HouseDog and PetRock in the following example.

INTERFACE-BASED POLYMORPHISM IN JAVA:

class Animal{
   int legs;
   double weight;

   public int getLegs(){return this.legs;}
   public double getWeight(){return this.weight}
}

interface Pet{
    public int getNumberOfDresses();
    public int getNumberOfToys();
}

class HouseDog extends Animal implements Pet{
   //... implementation here
}

class PetRock implements Pet{
   //a rock and a dog don't really have much in common.
   //But both can be treated as pets
}

QUESTION:

Again: Does python have an object-oriented way to achieve polymorphism without using inheritance?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:08:12+00:00Added an answer on June 14, 2026 at 9:08 pm

    Yes, they call it Duck Typing. There’s no need to formally implement an interface. Java-style interfaces don’t exist because Python has multiple inheritance, so if you want to define the interface, you just use a regular class as the supertype. But, generally, people avoid creating completely abstract base classes, and avail themselves of duck typing.

    For your example, one would just omit the Pet interface, but let HouseDog and PetRock both have getNumberOfDresses and getNumberOfToys methods. In polymorphic situations, Python checks for the existence of the methods, only when they are called, instead of checking the types at compile-time (which doesn’t exist) or load-time. The big advantage is in letting you rapidly prototype without worry about the formalisms while your implementation isn’t yet complete.

    If you do want formal type-checking, you can get it in a number of ways, but none is officially sanctioned.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a python webapp on Google App Engine and am using the
i have made an application using python and google app engine, the app works
I am developing an application on the Google App Engine using Python. I have
I implemented the experimental OAuth support for Google App Engine using Python, and have
I am learning to create a website using google app engine with python and
I'm working on a project, using Python running on Google App Engine. The project
I am using Google App Engine (python), I want my users to be able
I am using google app engine in python with a Jinja2 template engine. This
I'm using Google App Engine python. I want to know what browser the user
I'm using Google App Engine (Python) and Chanel api. Is it possible to show

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.