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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:57:45+00:00 2026-06-14T14:57:45+00:00

this is my code, I want to use eval() to get the rule status

  • 0

this is my code, I want to use eval() to get the rule status but eval() needs local variables, there is many classes that inherits the base class, so I need to rewrite get_stat() in every class.

I want to avoid this, an idea is to create dynamic variables in get_stat(),eg. class b dynamically creates variables a and b in func get_stat()

How should I create dynamic varables in function? or any other way to avoid this stupid idea. I use python 3.2.3, locals() does not work

class base(object):
    def check(self):
        stat = get_stat()
    def get_stat(self):
        pass

class b(base):
    rule = 'a > 5 and b < 3'
    a = 0
    b = 0

    def update_data(self, a, b):
        self.a = a
        self.b = b

    def get_stat(self):
        a = self.a
        b = self.b
        return eval(rule)

class b(base):
    rule = 'd > 5 and e < 3'
    d = 0
    e = 0

    def update_data(self, d, e):
        self.d = d
        self.e = e

    def get_stat(self):
        d = self.d
        e = self.e
        return eval(rule)
  • 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-14T14:57:46+00:00Added an answer on June 14, 2026 at 2:57 pm

    You can pass a dictionary to the eval() function containing the variables to evaluate the expression against:

    >>> eval("a + b", {"a": 2, "b": 3})
    5
    

    Pass self.__dict__ to give access to an object’s attributes:

    >>> class MyClass(object):
    ...     def __init__(self):
    ...             self.a = 2
    ...             self.b = 3
    ... 
    >>> obj = MyClass()
    >>> obj.__dict__
    {'a': 2, 'b': 3}
    >>> eval("a + b", obj.__dict__)
    5
    

    Links:

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

Sidebar

Related Questions

this is my code that I write it but I want to use LINQ
I want to send an SMS via intent, but when I use this code,
I want to use this C code in my C++ project : mysql.c :
I want to use this line of code: using (ADataContext _dc = new ADataContext(ConnectionString),
I want use this 1 for using Bar code or QR code scanner. I
I want to rewrite http://website.com/?slug=product_info.php&products_id=32 to http://website.com/product_info/32 and I use this code, add_filter('rewrite_rules_array','wp_insertMyRewriteRules'); add_filter('query_vars','wp_insertMyRewriteQueryVars');
Assuming sr is an IEnumerable<string> , I want to use code like this to
I want to use pjsipDll.dll in a c++ code. I got this dll from
I want to add an external css sheet and I use this JS code:
i want change style linkbutton row that the showing. i want use this code

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.