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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:45:03+00:00 2026-05-16T03:45:03+00:00

What happens internally when I press Enter ? My motivation for asking, besides plain

  • 0

What happens internally when I press Enter?

My motivation for asking, besides plain curiosity, is to figure out what happens when you

from sympy import *

and enter an expression. How does it go from Enter to calling

__sympifyit_wrapper(a,b)

in sympy.core.decorators? (That’s the first place winpdb took me when I tried inspecting an evaluation.) I would guess that there is some built-in eval function that gets called normally, and is overridden when you import sympy?

  • 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-16T03:45:04+00:00Added an answer on May 16, 2026 at 3:45 am

    All right after playing around with it some more I think I’ve got it.. when I first asked the question I didn’t know about operator overloading.

    So, what’s going on in this python session?

    >>> from sympy import *
    >>> x = Symbol(x)
    >>> x + x
    2*x
    

    It turns out there’s nothing special about how the interpreter evaluates the expression; the important thing is that python translates

    x + x

    into

    x.__add__(x)

    and Symbol inherits from the Basic class, which defines __add__(self, other) to return Add(self, other). (These classes are found in sympy.core.symbol, sympy.core.basic, and sympy.core.add if you want to take a look.)

    So as Jerub was saying, Symbol.__add__() has a decorator called _sympifyit which basically converts the second argument of a function into a sympy expression before evaluating the function, in the process returning a function called __sympifyit_wrapper which is what I saw before.

    Using objects to define operations is a pretty slick concept; by defining your own operators and string representations you can implement a trivial symbolic algebra system quite easily:

    symbolic.py —

    class Symbol(object):
        def __init__(self, name):
            self.name = name
        def __add__(self, other):
            return Add(self, other)
        def __repr__(self):
            return self.name
    
    class Add(object):
        def __init__(self, left, right):
            self.left = left
            self.right = right
        def __repr__(self):
            return self.left + '+' + self.right
    

    Now we can do:

    >>> from symbolic import *
    >>> x = Symbol('x')
    >>> x+x
    x+x
    

    With a bit of refactoring it can easily be extended to handle all basic arithmetic:

    class Basic(object):
        def __add__(self, other):
            return Add(self, other)
        def __radd__(self, other): # if other hasn't implemented __add__() for Symbols
            return Add(other, self)
        def __mul__(self, other):
            return Mul(self, other)
        def __rmul__(self, other):
            return Mul(other, self)
        # ...
    
    class Symbol(Basic):
        def __init__(self, name):
            self.name = name
        def __repr__(self):
            return self.name
    
    class Operator(Basic):
        def __init__(self, symbol, left, right):
            self.symbol = symbol
            self.left = left
            self.right = right
        def __repr__(self):
            return '{0}{1}{2}'.format(self.left, self.symbol, self.right)
    
    class Add(Operator):
        def __init__(self, left, right):
            self.left = left
            self.right = right
            Operator.__init__(self, '+', left, right)
    
    class Mul(Operator):
        def __init__(self, left, right):
            self.left = left
            self.right = right
            Operator.__init__(self, '*', left, right)
    
    # ...
    

    With just a bit more tweaking we can get the same behavior as the sympy session from the beginning.. we’ll modify Add so it returns a Mul instance if its arguments are equal. This is a bit trickier since we have get to it before instance creation; we have to use __new__() instead of __init__():

    class Add(Operator):
        def __new__(cls, left, right):
            if left == right:
                return Mul(2, left)
            return Operator.__new__(cls)
        ...
    

    Don’t forget to implement the equality operator for Symbols:

    class Symbol(Basic):
        ...
        def __eq__(self, other):
            if type(self) == type(other):
                return repr(self) == repr(other)
            else:
                return False
        ...
    

    And voila. Anyway, you can think of all kinds of other things to implement, like operator precedence, evaluation with substitution, advanced simplification, differentiation, etc., but I think it’s pretty cool that the basics are so simple.

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

Sidebar

Related Questions

I've got a buffer overrun I absolutely can't see to figure out (in C).
Internally, Firefox will JSON encode an object passed via postMessage to and from the
What happens if I use SHGetFolderPath api call in a 32 bit system with
What happens to exceptions raised while in myMethod: if it is invoked via NSObject's
What happens to the name/value pairs stored inside a form's resx file? Are they
What happens when the Office 2003 PIA prerequisite and launch condition in a Windows
This happens repeatedly and is very annoying. I upload some PHP code to a
What happens in the memory when a class instantiates the following object? public class
What happens on a Windows box once you add more drives than can fit
what happens if an user trying to read HttpContext.Current.Cache[key] while the other one trying

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.