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

  • Home
  • SEARCH
  • 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 6382929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:35:11+00:00 2026-05-25T02:35:11+00:00

I would like to register a class (not an instance) when it’s created… but

  • 0

I would like to register a class (not an instance) when it’s created… but without importing it.

Basically, I want to do what’s described here:

How to auto register a class when it's defined

… but without having to import the registered class anywhere.

Here’s the issue: At a certain point of my application, I need to run certain system commands (let’s stick with reboot, halt… for the example). I have created a “base” (abstract-mmnnno-not-really looking) class “Command” and a “CommandManager” which will receive an string with the command to run, create the appropriate instance for said command, queue it and dispatch the commands when needed (or whenever it can). All the instances of the “Command” class will overwrite an “execute” method that actually executes the command. If I want to “reboot”, I would extend the “Command” class on a “RebootCommand” class, and overwrite that “execute” making the call to the external application “shutdown -r” or “reboot”.

The final goal is being able to do something like:

CommandManager.execute("reboot")

And the system will reboot.

In order to do so, the “reboot” string has to be registered in a dictionary inside the CommandManager. That dictionary will have “reboot” (string) as the key and the class RebootCommand as the value. The CommandManager will receive the string “reboot“, will go to it’s “commandDictionary”, create an instance of a RebootCommand class and call its execute() method (thus, rebooting the system).

Something like:

#------------- CommandManager.py -------------
@classmethod
def execute(cls, parameter):
    if parameter in cls.validCommands:
        tmpCommand = cls.validCommands[parameter]()
        tmpCommand.execute()
#---------------------------------------------

In order to do the registering, the RebootCommand class-object has to be created, and in order to do that, I have to import it somewhere. But I don’t want to. I don’t need to. I just need the RebootCommand class object created, so it will execute the __new__ method for the Command MetaClass that is where the registering takes place (as detailed in the answer to the other question above).

Is there any way to do it? I’ve tried importing the actual commands (the classes inheriting from Command) it in the __init__.py, but then, if I create a new command, I have to add it to the __init__.py and I would like to avoid that (so another programmer can create a new command without worrying about forgetting adding the class to the __init__.py)

Here’s my directory structure:

commands/
    __init__.py
    CommandManager.py
    Command.py
    RebootCommand.py

And here, the contents of Command and RebootCommand that make the registering:

#--------------- Command.py -------------------
def register(newCommand):
    if newCommand and newCommand._command:
        import CommandManager
        CommandManager.CommandManager.registerCommand(newCommand._command, newCommand)


# Fancy registering! https://stackoverflow.com/questions/5189232/how-to-auto-register-a-class-when-its-defined
class CommandMetaClass(type):
    def __new__(cls, clsname, bases, attrs):
        newCommand = super(cls, CommandMetaClass).__new__(cls, clsname, bases, attrs)
        register(newCommand)  # here is your register function
        return newCommand

class Command(object):
    __metaclass__ = CommandMetaClass
    _command = None
#-----------------------------------------------------

and…

#--------------- RebootCommand.py -------------------
class RebootCommand(Command.Command):
    _command = "reboot"
#---------------------------------------------------

The register process is properly executed if I open __init__.py and I write:

import RebootCommand

(at this point the RebootCommand class object is created,

CommandMetaClass.__new__

function is run, “reboot” is registered as the RebootCommand class, and everyone is happy)

but I’d like to avoid having to do that (touching __init__.py), if possible

I have tried importing with the * wildcard without luck.

To sum up, I’d like that another programmer that wants to implement a new Command, just has to create a new class inheriting from Command but without having to add it to __init__.py

I know it’s nothing that can’t be fixed with a comment in the Command.py file, saying something like “any class inheriting from this have to be added in __init__.py” but I’m curious to know if I can do it in a more “elegant” way.

  • 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-25T02:35:11+00:00Added an answer on May 25, 2026 at 2:35 am

    I feel that your approach is a bit complicated to implement the functionality you want. My suggestion is to you use a strategy like this:

    1. Name your modules so that the command classes are defined in files named after the commands they implement. For example, the RebootCommand class goes into the file commands/reboot.py.
    2. Every command module defines a top-level variable command_cls that contains a reference to the class implementing the command. commands/reboot.py would therefore contain code like this:

      class RebootCommand:
        def execute(self):
          # [...]
      
      command_cls = RebootCommand
      
    3. To execute a command, you use the __import__ function to dynamically load your module. Your CommandManager.py would logically live in a file outside the commands directory (which is reserved for command modules) and would be implemented like the following:

      class CommandManager:
        @classmethod
        def execute(cls, command, *args, **kw):
          # import the command module
          commands_mod = __import__("commands", globals(), locals(), [command])
          mod = getattr(commands_mod, command)
          return mod.command_cls(*args, **kw).execute()
      

    EDIT: Seems I confused the usage of the imp module. Updated the code to use __import__.

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

Sidebar

Related Questions

I have a class that implements multiple interfaces. I would like to register these
I have php scripts that do things like register, login, upload. I would like
Problem Language: C# 2.0 or later I would like to register context handlers to
I would like to convert this fluent approach to xml: container.Register( AllTypes.FromAssemblyNamed(Company.DataAccess) .BasedOn(typeof(IReadDao<>)).WithService.FromInterface(), AllTypes.FromAssemblyNamed(Framework.DataAccess.NHibernateProvider)
I've a website where users can register. I would like to prevent all special
Would like to make anapplication in Java that will not automatically parse parameters used
I noticed the function Object.factory(char[] className) in D. But it does not work like
I would like to create a singleton class that is instantiated once in each
I would like to implement what I know as a CVAR System, I'm not
I would like to add some dynamic behavior to an application, preferably without resorting

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.