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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:04:25+00:00 2026-06-05T18:04:25+00:00

I am trying to define a number of classes based on an abstract base

  • 0

I am trying to define a number of classes based on an abstract base class. Each of these classes basically defines a cell shape for a visualisation package. The cell is comprised of a number of vertices (points) and each subclass will require a different number of points. Each class can be thought of as a container for a fixed number of point coordinates.

As an example, consider the base class Shape, which is simply a container for a list of coordinates:

class Shape(object):
    """Cell shape base class."""
    def __init__(self, sequence):
        self.points = sequence

    @property
    def points(self):
        return self._points

    @points.setter
    def points(self, sequence):
        # Error checking goes here, e.g. check that `sequence` is a
        # sequence of numeric values.
        self._points = sequence

Ideally I want to be able to define, say, a Square class, where the points.setter method checks that sequence is of length four. Furthermore I would like a user to not be able to instantiate Shape. Is there a way I can define Shape to be an abstract base class? I have tried changing the definition of shape to the following:

import abc

class Shape(object):
    """Cell shape base class."""

    __metaclass__ = abc.ABCMeta

    def __init__(self, sequence):
        self.points = sequence

    @abc.abstractproperty
    def npoints(self):
        pass

    @property
    def points(self):
        return self._points

    @points.setter
    def points(self, sequence):
        # Error checking goes here...
        if len(sequence) != self.npoints:
            raise TypeError('Some descriptive error message!')

        self._points = sequence

This requires subclasses to define the property npoints. I can then define a class Square as

class Square(Shape):
    @property
    def npoints(self):
        return 4

However, this would be rather tedious to implement for a large number of sublcasses (and with more than one property to implement). I was hoping to define a class factory which would create my subclasses for me, something along the lines of:

def Factory(name, npoints):
    return type(name, (Shape,), dict(npoints=npoints))

Triangle = Factory('Triangle', 3)    
Square = Factory('Square', 4)
# etc...

Is this class factory function a valid approach to take, or am I clobbering the npoints property? Is it better to replace the call to type with something more verbose like:

def Factory(name, _npoints):
    class cls(Shape):
        @property
        def npoints(self):
            return _npoints
    cls.__name__ = name
    return cls

An alternative approach would be to define a class attribute _NPOINTS and change the npoints
property of Shape to

@property
def npoints(self):
    return _NPOINTS

However, then I loose the benefit of using an abstract base class since:

  • I can’t see how to define a class attribute using type, and
  • I don’t know how to define an abstract class attribute.

Does anyone have any thoughts on the best way to implement this abstract base class and class factory function, or even an altogether better design?

  • 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-05T18:04:28+00:00Added an answer on June 5, 2026 at 6:04 pm

    Without knowing more about your project, I cannot give specific advice on the general design. I will just provide a few more general hints and thoughts.

    1. Dynamically generated classes are often a sign that you don’t need separate classes at all – simply write a single class that incorparates all the functionality. What’s the problem with a Shape class that gets it’s properties at instantiation time? (Of course there are reasons to use dynamically generated classes – the namedtuple() factory function is one example. I couldn’t find any specific reasons in your question, however.)

    2. Instead of using abstract base classes, you often simply document the intended interface, and than write classes conforming to this interface. Due to the dynamic nature of Python, you don’t strictly need a common base class. There are often other advantages to a common base class – for example shared functionality.

    3. Only check for application code errors if not doing so leads to strange errors in unrelated places. If, say, your function expects an iterable, simply assume you got an iterable. If the user passed in something else, you code will fail when it tries to iterate the passed in object anyway, and the error message will usually be enough for the application developer to understand the error.

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

Sidebar

Related Questions

I'm trying to implement a number of classes based on a a common class
I'm trying to define base class, which contains typedef's only. template<typename T> class A
I am trying to set up Entity Inheritance using JPA annotations and abstract classes.
I am trying to define an interface and classes which implement the interface as
I'm trying to design a code where one guess a number. I defined the
Im trying to define a select tag in my view. My view looks like
I'm trying to define a delegate function that will return an IEnumerable. I'm having
I'm trying to define a new shortcode in WordPress, and I get the following
I'm trying to define a way to simulate an case on accessing into a
I'm trying to define an action for a model Bar -- but I only

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.