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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:40:11+00:00 2026-05-11T17:40:11+00:00

In Python, I’d like to write a function that would pretty-print its results to

  • 0

In Python, I’d like to write a function that would pretty-print its results to the console if called by itself (mostly for use interactively or for debugging). For the purpose of this question, let’s say it checks the status of something. If I call just

check_status()

I would like to see something like:

Pretty printer status check 0.02v
NOTE: This is so totally not written for giant robots
=================================
System operational: ... ok
Time to ion canon charge is 9m 21s
Booster rocket in AFTERBURNER state
Range check is optimal
Rocket fuel is 10h 19m 40s to depletion
Beer served is type WICKSE LAGER, chill optimal
Suggested catchphrase is 01_FIGHTING_SPIRIT_GOGOGO
Virtual ... on

However, I would also like it to pass the output as a list if I call it in the context of a variable assignment:

not_robot_stat = check_status()
print not_robot_stat
>>> {'cond_op': 1, 't_canoncharge': 1342, 'stage_booster': 5, 'range_est_sigma': 0.023, 'fuel_est': 32557154, 'beer_type': 31007, 'beer_temp': 2, 'catchphrase_suggestion': 1023, 'virtual_on': 'hell yes'}

So… is there a way to dynamically know, within a function, whether its output is being assigned? I’d like to be able to do this without resorting param passing, or writing another function dedicated for this. I’ve Googled for a bit, and from what little I can tell it looks like I’d have to resort to playing wth the bytecode. Is that really necessary?

  • 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-11T17:40:11+00:00Added an answer on May 11, 2026 at 5:40 pm

    New Solution

    This is a new that solution detects when the result of the function is used for assignment by examining its own bytecode. There is no bytecode writing done, and it should even be compatible with future versions of Python because it uses the opcode module for definitions.

    import inspect, dis, opcode
    
    def check_status():
    
        try:
            frame = inspect.currentframe().f_back
            next_opcode = opcode.opname[ord(frame.f_code.co_code[frame.f_lasti+3])]
            if next_opcode == "POP_TOP": 
                # or next_opcode == "RETURN_VALUE":
                # include the above line in the if statement if you consider "return check_status()" to be assignment
                print "I was not assigned"
                print "Pretty printer status check 0.02v"
                print "NOTE: This is so totally not written for giant robots"
                return
        finally:
            del frame    
    
        # do normal routine
    
        info = {'cond_op': 1, 't_canoncharge': 1342, 'stage_booster': 5}
    
        return info
    
    # no assignment    
    def test1():
        check_status()
    
    # assignment
    def test2():
        a = check_status()
    
    # could be assignment (check above for options)
    def test3():
        return check_status()
    
    # assignment
    def test4():
        a = []
        a.append(check_status())
        return a
    

    Solution 1

    This is the old solution that detects whenever you are calling the function while debugging under python -i or PDB.

    import inspect
    
    def check_status():
        frame = inspect.currentframe()
        try:
            if frame.f_back.f_code.co_name == "<module>" and frame.f_back.f_code.co_filename == "<stdin>":
                print "Pretty printer status check 0.02v"
                print "NOTE: This is so totally not written for giant robots"
        finally:
            del frame
    
        # do regular stuff   
        return {'cond_op': 1, 't_canoncharge': 1342, 'stage_booster': 5}
    
    def test():
        check_status()
    
    
    >>> check_status()
    Pretty printer status check 0.02v
    NOTE: This is so totally not written for giant robots
    {'cond_op': 1, 't_canoncharge': 1342, 'stage_booster': 5}
    
    >>> a=check_status()
    Pretty printer status check 0.02v
    NOTE: This is so totally not written for giant robots
    
    >>> a
    {'cond_op': 1, 't_canoncharge': 1342, 'stage_booster': 5}
    
    test()
    >>>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 207k
  • Answers 207k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Aren't you supposed to import like: from mysite.myapp.models import Organization… May 12, 2026 at 9:18 pm
  • Editorial Team
    Editorial Team added an answer Also trying adding super.data = value May 12, 2026 at 9:18 pm
  • Editorial Team
    Editorial Team added an answer int row=odd; should be int row=odd-1; You are later indexing… May 12, 2026 at 9:18 pm

Related Questions

In Python, I want to make selected instance attributes of a class be readonly
In Python, I can do: list = ['a', 'b', 'c'] ', '.join(list) # 'a,
In Python, I've got a list of dictionaries that looks like this: matchings =
In Python, I can compile a regular expression to be case-insensitive using re.compile :
In python, I can do something like this: List=[3, 4] def Add(x, y): return

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.