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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:17:26+00:00 2026-05-17T01:17:26+00:00

I have following code: class EntityBase (object) : __entity__ = None def __init__ (self)

  • 0

I have following code:

class EntityBase (object) :
    __entity__ = None

    def __init__ (self) :
        pass

def entity (name) :
    class Entity (EntityBase) :
        __entity__ = name

        def __init__ (self) :
            pass

    return Entity

class Smth (entity ("SMTH")) :
    def __init__ (self, a, b) :
        self.a = a
        self.b = b

# added after few comments -->
def factory (tag) :
    for entity in EntityBase.__subclasses__ () :
        if entity.__entity__ == tag :
            return entity.__subclasses__ ()[0]

    raise FactoryError (tag, "Unknown entity")

s = factory ("SMTH") (1, 2)
print (s.a, s.b)
# <--

Now in factory I can get all subclasses of EntityBase, find concrete subclass for “SMTH” and create it.

Is this valid approach or maybe I something misunderstood and doing wrong?

  • 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-17T01:17:27+00:00Added an answer on May 17, 2026 at 1:17 am

    I would do this with a decorator. Also, storing the entity -> subclass map in a dictionary lets you replace a linear scan with a dict lookup.

    class EntityBase(object):
        _entity_ = None
        _entities_ = {}
    
        @classmethod
        def factory(cls, entity):
            try:
                return cls._entities_[entity]
            except KeyError:
                raise FactoryError(tag, "Unknown entity")
    
        @classmethod
        def register(cls, entity):
            def decorator(subclass):
                cls._entities_[entity] = subclass
                subclass._entity_ = entity
                return subclass
            return decorator
    
     factory = EntityBase.factory
     register = EntityBase.register
    
     @register('Smith')
     class Smith(EntityBase):
         def __init__(self, a, b):
             self.a = a
             self.b = b
    
     s = factory('Smith')(1, 2)
    

    I’m not sure if the __entity__ attribute is actually useful to you of if you were just using it to implement the linear scan. I left it in but if you took it out, then the classes associated with entity wouldn’t even need to inherit from EntityBase and you could rename it to something like Registry. This shallows up your inheritance tree and opens the possibility of using on classes that aren’t related through common descent.

    Depending on what your use case is, a better way to do it might just be

    factory = {}
    
    class Smith(object):
        def __init__(self, a, b):
            self.a = a
            self.b = b
    factory['Smith'] = Smith
    
    class Jones(object):
        def __init__(self, c, d):
             self.c = c
             self.d = d
    factory['Jones'] = Jones
    
    s = factory['Smith'](1, 2)
    j = factory['Jones'](3, 4)
    

    The decorator is fancier and let’s us feel nice and fancy about ourselves but the dictionary is plain, useful and to the point. It is easy to understand and difficult to get wrong. Unless you really need to do something magic, then I think that that’s the way to go. Why do you want to do this, anyway?

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

Sidebar

Related Questions

I have the following code: class A(object): def __init__(self): self.name = A super(A, self).__init__()
I have the following example code: class A(object): def __init__(self, id): self.myid = id
In Python, consider I have the following code: class SuperClass(object): def __init__(self, x): self.x
I have the following code : class C: def __init__(self, dx = 1): self._dx
I have the following code class Board: def __init__(self, size=7): self._size = size self._list,
I have the following code: class IncidentTag: def __init__(self,tag): self.tag = tag def equals(self,obj):
I have the following code: class MyClass: def __private(self): print Hey man! This is
I have following code class User attr_accessor :name end u = User.new u.name =
I have the following code: class Employee { friend string FindAddr( list<Employee> lst,string name
in c++ i have following code class Foobar{ public: Foobar * operator()(){ return new

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.