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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:33:37+00:00 2026-05-26T08:33:37+00:00

I’m wondering if there exists a python module that would allow me to do

  • 0

I’m wondering if there exists a python module that would allow me to do something like this:

x = MagicNumber()
x.value = 3
y = 2 * (x + 2) ** 2 - 8
print y   # 42
x.value = 2
print y   # 24

So MagicNumber would implement all the special operator methods, and they would all return instances of MagicNumber, while keeping track of what operations are performed. Is there such a class?

EDIT: clarification

I want to use this in a module that should remember a lot of parameters of some arbitrary calculation that the user wishes to perform. So the user will set the parameters and then use them to produce his result. Then if he decides he’d like to alter a parameter, the change is reflected in his result immediately. So a very simplified usage session with only one parameter instance would look like:

p = MyParams()
p.distance = 13.4           # I use __getattr__ and __setattr__ such that
p.speed = 3.14              # __getattr__ returns MagicNumber instances
time = p.distance / p.speed

EDIT 2: more clarification

Okay, I’ll do what I should have done from the start. I’ll provide context.

You are an engineer and you’re to produce a LaTeX document detailing the workings and properties of some prototype gadget. It is a task you’ll do repeatedly for different prototypes. You write a small LaTeX python interface. For each prototype you create a python module that generates the requisite document. In it you type out the LaTeX code while calculating variables as they are needed, so that the calculations are in context. After a while you notice two problems:

  1. The number of variables and parameters makes locals a mess and the variable names are hard to remember. You should group them into categories to keep track of them all.
  2. You sometimes need to redo the same calculation, which is spread over the last couple of chapters and a dozen lines, with one or more parameters changed. You should find some way to avoid code duplication.

Out of this problem comes the original question.

  • 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-26T08:33:38+00:00Added an answer on May 26, 2026 at 8:33 am

    Something like this?

    import operator
    
    MAKE_BINARY  = lambda opfn : lambda self,other : BinaryOp(self, asMagicNumber(other), opfn)
    MAKE_RBINARY = lambda opfn : lambda self,other : BinaryOp(asMagicNumber(other), self, opfn)
    
    class MagicNumber(object):
        __add__  = MAKE_BINARY(operator.add)
        __sub__  = MAKE_BINARY(operator.sub)
        __mul__  = MAKE_BINARY(operator.mul)
        __radd__ = MAKE_RBINARY(operator.add)
        __rsub__ = MAKE_RBINARY(operator.sub)
        __rmul__ = MAKE_RBINARY(operator.mul)
        # __div__  = MAKE_BINARY(operator.div)
        # __rdiv__ = MAKE_RBINARY(operator.div)
        __truediv__ = MAKE_BINARY(operator.truediv)
        __rtruediv__ = MAKE_RBINARY(operator.truediv)
        __floordiv__ = MAKE_BINARY(operator.floordiv)
        __rfloordiv__ = MAKE_RBINARY(operator.floordiv)
    
        def __neg__(self, other):
            return UnaryOp(self, lambda x : -x)
    
        @property
        def value(self):
            return self.eval()
    
    class Constant(MagicNumber):
        def __init__(self, value):
            self.value_ = value
    
        def eval(self):
            return self.value_
    
    class Parameter(Constant):
        def __init__(self):
            super(Parameter, self).__init__(0.0)
    
        def setValue(self, v):
            self.value_ = v
    
        value = property(fset=setValue, fget=lambda self: self.value_)
    
    class BinaryOp(MagicNumber):
        def __init__(self, op1, op2, operation):
            self.op1 = op1
            self.op2 = op2
            self.opn = operation
    
        def eval(self):
            return self.opn(self.op1.eval(), self.op2.eval())
    
    
    class UnaryOp(MagicNumber):
        def __init__(self, op1, operation):
            self.op1 = op1
            self.operation = operation
    
        def eval(self):
            return self.opn(self.op1.eval())
    
    asMagicNumber = lambda x : x if isinstance(x, MagicNumber) else Constant(x)
    

    And here it is in action:

    x = Parameter()
    # integer division
    y = 2*x*x + 3*x - x//2
    
    # or floating division
    # y = 2*x*x + 3*x - x/2
    
    x.value = 10
    print(y.value)
    # prints 225
    
    x.value = 20
    print(y.value)
    # prints 850
    
    # compute a series of x-y values for the function
    print([(x.value, y.value) for x.value in range(5)])
    # prints [(0, 0), (1, 5), (2, 13), (3, 26), (4, 42)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.