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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:26:14+00:00 2026-05-16T10:26:14+00:00

Though the question is very specific, I’d also really appreciate general advice and other

  • 0

Though the question is very specific, I’d also really appreciate general advice and other approaches that would make my question moot. I’m building a collection of AI programs, and many of the functions and classes need to deal with a lot of different states and actions that cause transitions between states, so I need a way to represent states and actions. Please note that I’m not building a simple state machine, but rather a number of different programs (agents) that all take states and return actions as a way of interacting with an environment.

I could use strings, but that’s messy if a particular algorithm needs to associate additional information with a state or action, and comparing strings over and over again in long-running programs is wasted overhead. The same sorts of problems arise with other kinds of constants. So my initial idea is to use nested classes, like so:

class DerivedAgent(Agent):
    class StateA(State): pass
    class StateB(State): pass
    ...
    def do_something(state):
        if state is self.StateA:
           ...

This works fairly well, but if there are a number of states and actions, it can take up a lot of space to declare them all, and all of the pass statements are annoying. I’d like to be able to do something like…

class DerivedAgent(Agent):
    states("StateA", "StateB", "StateC", ...)

But I don’t see a way to have the states method add the newly-created types to the DerivedAgent class. I think I might be able to do it with the inspect module, but that feels like it’s going too far for a small convenience. Is using types like this a bad idea? Is there a much more elegant approach? Code outside of the agent classes will need to be able to access the states and actions, and putting states into the module namespace isn’t a good option because a given module might have several agents in it.

  • 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-16T10:26:14+00:00Added an answer on May 16, 2026 at 10:26 am

    You could use meta classes so that you would end up with code like:

    class DerivedAgent(Agent):
        __states__ = ['StateA', 'StateB', ...]
    

    for example:

    class AgentMeta(type):
        def __new__(meta, classname, bases, classdict):
            for clsname in classdict['__states__']:
                classdict[clsname] = type(clsname, (State,), {})
            return type.__new__(meta, classname, bases, classdict))
    

    then, just rewrite your Agent class so that it has the line

    #python3.x
    class Agent(Base1, Base2, ..., BaseN, metaclass=AgentMeta):
        #everything else unchanged
    
    # 2.2 <= python <= 2.7 
    class Agent(Base1, Base2, ..., BaseN):
        __metaclass__ = AgentMeta
        #everything else unchanged
    

    If you don’t want to change the Agent class, you can just include the approriate declaration of metaclass in each subclass of it that you create.

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

Sidebar

Related Questions

Sorry for a very specific question, bot why VS2008 and VS2010 crashes with this
This question is related to this one , though I think I was a
I've seen the question (and answer) when posed for MS SQL Server , though
Reading through this question on multi-threaded javascript, I was wondering if there would be
I was browsing the Hidden Features of C# question and thought I would try
a question that a co-interviewer asked in interviews. I always thought it was a
This question is a general one, and I've already posted a version of it
Here's a very broad question: I've been building a small MVC framework in PHP,
My question is essentially a simple one, though I'm looking for as in-depth an
[Updated organization and content for clarity] The Real Question What would be a good

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.