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 886415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:03:51+00:00 2026-05-15T13:03:51+00:00

I have been reading documentation describing class inheritance, abstract base classes and even python

  • 0

I have been reading documentation describing class inheritance, abstract base classes and even python interfaces. But nothing seams to be exactly what I want. Namely, a simple way of building virtual classes. When the virtual class gets called, I would like it to instantiate some more specific class based on what the parameters it is given and hand that back the calling function. For now I have a summary way of rerouting calls to the virtual class down to the underlying class.

The idea is the following:

class Shape:
    def __init__(self, description):
        if   description == "It's flat":  self.underlying_class = Line(description)
        elif description == "It's spiky": self.underlying_class = Triangle(description)
        elif description == "It's big":   self.underlying_class = Rectangle(description)
    def number_of_edges(self, parameters):
        return self.underlying_class(parameters)

class Line:
    def __init__(self, description):
        self.desc = description
    def number_of_edges(self, parameters):
        return 1

class Triangle:
    def __init__(self, description):
        self.desc = description
    def number_of_edges(self, parameters):
        return 3

class Rectangle:
    def __init__(self, description):
        self.desc = description
    def number_of_edges(self, parameters):
        return 4

shape_dont_know_what_it_is = Shape("It's big")
shape_dont_know_what_it_is.number_of_edges(parameters)

My rerouting is far from optimal, as only calls to the number_of_edges() function get passed on. Adding something like this to Shape doesn’t seam to do the trick either:

def __getattr__(self, *args):
    return underlying_class.__getattr__(*args)

What I am doing wrong ? Is the whole idea badly implemented ? Any help greatly appreciated.

  • 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-05-15T13:03:52+00:00Added an answer on May 15, 2026 at 1:03 pm

    I would prefer doing it with a factory:

    def factory(description):
        if   description == "It's flat":  return Line(description)
        elif description == "It's spiky": return Triangle(description)
        elif description == "It's big":   return Rectangle(description)
    

    or:

    def factory(description):
        classDict = {"It's flat":Line("It's flat"), "It's spiky":Triangle("It's spiky"), "It's big":Rectangle("It's big")}
        return classDict[description]
    

    and inherit the classes from Shape

    class Line(Shape):
        def __init__(self, description):
            self.desc = description
        def number_of_edges(self, parameters):
            return 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 495k
  • Answers 495k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use each_cons: { "000000001" => "0.00", "000000002" => "66.68", "000000010"… May 16, 2026 at 11:13 am
  • Editorial Team
    Editorial Team added an answer Last() is optimized when there isn't a predicate... it could… May 16, 2026 at 11:13 am
  • Editorial Team
    Editorial Team added an answer Making Silverlight capable of becoming a spamming platform out-of-the-box would… May 16, 2026 at 11:13 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have been reading the rsync documentation for a few hours, but I can't
I have been reading the MSDN documentation on subclassing and I have been successful
I have been reading through the documentation on the JavaScriptMVC framework and it looks
I have been reading thru the documentation however it is not immediatly clear to
I'm reading in a file with Python's csv module, and have Yet Another Encoding
I'm new to Python and I have been trying to search through html with
I have been reading tutorials from several different places like http://css-tricks.com/scrollfollow-sidebar/ or http://jqueryfordesigners.com/fixed-floating-elements/ and
I have been reading the JavaScript Scripting Guide for Quicktime and in there it
I have been coding my PHP applications in Notepad. I want to take it
So I have been looking at Google Maps and they have the feature of

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.