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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:33:52+00:00 2026-06-15T21:33:52+00:00

I am using python 2.6.6 and I need to overload the default python print

  • 0

I am using python 2.6.6 and I need to overload the default python print function. I need to do it because this code may be used on a system where a built-in function has to be used to generate output, otherwise no output is displayed.

So, just for example, if you have a python script like this:

from __future__ import print_function

def NewPrint(Str):
    with open("somefile.txt","a") as AFile:
        AFile.write(Str)

def OverloadPrint():
    global print
    print = NewPrint

OverloadPrint()
print("ha")

It works fine. The input to the “overloaded” print is in the file specified by NewPrint.

Now with that in mind I would like to be able to run the couple of lines above and have print to do what NewPrint does during the entire execution of the script. Right now if I call a function from another module that uses print it will use the built-in print and not the one I just overwrote. I guess this has something to do with namespaces and built-ins, but my python is not good enough.

Edit:

I tried to keep simple, but looks like this caused more confusion…

  1. The function that overloads print will print to a GUI, so no need to care about redirection IF the function is overloaded.
  2. I know my example does not do what the print function actually does. A better example of how I am thinking of coding it is (still not great I know):

    def hli_print(*args, **kw):
        """
        print([object, ...], sep=' ', end='\n', file=sys.stdout)
        """
        sep = kw.get('sep', ' ')
        end = kw.get('end', '\n')
        File = kw.get('file', sys.stdout)
        args = [str(arg) for arg in args]
        string = sep.join(args) + end
        File.write(string)
        hli_Print(string)
    
  3. From 2 above you can see the function I have to use to print to the GUI “hli_Print” it is a C++ function exposed through a swig wrapper.

  4. We only use the standard library and our own swig wrappers, also our developers use print as a function (getting used to 3.X). So I did not really worry much about some other module calling print and having something else instead.

From all the comments I guess that just using some print_() function instead of print() (which is what we currently do) may be the best, but I got really curious to see if in python it would be possible to do what I described.

  • 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-15T21:33:54+00:00Added an answer on June 15, 2026 at 9:33 pm

    I don’t think your question makes any sense.

    First, if you’re running Python 2.6, everything you import, etc., will be using print statements, even if your own module is using the print function. So, overloading the function will not affect anything else.

    Second, you say “I need to do it because this code may be used on a system where a built-in function has to be used to generate output, otherwise no output is displayed.” Well, your NewPrint is not a built-in function, so this won’t help anyway.

    It’s also worth noting that your NewPrint doesn’t implement most of the functionality of the print function, and even the bit that it does implement, it does wrong (print(s) will print s followed by a newline). So, if you did replace the builtin print function with yours, you’d just end up breaking most of your own code and any stdlib/third-party code you depend on.

    It may be that you can accomplish what you want by creating a file-like object that replaces sys.stdout. Otherwise, I can’t see how anything could work. For example:

    class FakeStdOut(object):
        # … lots of other stuff to implement or inherit
        def write(s):
            with open("somefile.txt", "a") as f:
                f.write(s)
    
    def OverloadPrint():
        sys.stdout = FakeStdOut()
    

    But even if this works, it probably isn’t what you really want. For a quick&dirty script, on a platform with a defective shell, this is sometimes a handy idea. But otherwise, it will probably cause you more trouble in the long run than coming up with a better solution. Here’s just a few things that can go wrong (just as examples, not an exhaustive list)

    • If you ever want to change the file the output goes to, you have to modify the script. If you instead used >> in the shell, you could just call the script differently.
    • Someone reading or debugging your code (like, say, you, three months after you forgot how it worked) will be surprised by what’s going on.
    • Some stdlib/third-party/coworker/etc. code you call will check that stdout is a tty before you make the change, and configure itself for interactive output.
    • Some code will print before you got a chance to redirect, and you’ll spend hours trying to figure out how to reorder things to work around the problem.
    • You have to know how to implement a ‘file-like object’ completely—and that concept is not fully defined in 2.6—or it will break with some code.
    • Somewhere, there’s some code that you thought was printing, but it was actually, say, logging or writing to sys.stderr or doing something else, so you’ve given yourself a false sense of security that you’re now logging everything in somefile.txt, and won’t discover otherwise until 6 months later, when you desperately need that missing information to debug a problem at a customer site.

    Since you’ve edited the question, here’s some further responses:

    From all the comments I guess that just using some print_() function instead of print()

    Yes, that’s a more reasonable option. But I probably wouldn’t call it print_. And it’s simpler to put the “do or do not” logic inside the function, instead of swapping implementations in and out of the global name (especially since you’re going to screw that up at some point if your code isn’t all in one big module).

    I worked on a project with a similar use case: We had messages we wanted to go to the syslogs, and also go to a GUI “log window” if it was open. So we wrote a glog function that wrapped that up, and nobody complained that they wanted to write print instead. (In fact, at least one guy on the team was very happy that he could use print for quick-and-dirty printouts while debugging without affecting the real output, especially when he had to debug the GUI logging code.)

    But that was just because we didn’t have any experience with the new (back then) logging module. Nowadays, I think I’d create a logging Handler implementation that writes to the GUI window, and just add that handler, and use the standard logging methods everywhere. And it sounds like that might be the best option for you.

    Also, one last probably-irrelevant side issue:

    We only use the standard library and our own swig wrappers, also our developers use print as a function (getting used to 3.X).

    So why not use 3.x in the first place? Obviously 3.x has the actual 3.x standard library, instead of something kind of close to the 3.x standard library if you do some __future__ statements, and SWIG works with 3.x…

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

Sidebar

Related Questions

I need to convert string into ASCII code. I'm using python. I did as
I need to use Python because: I have implemented many scripts and libraries using
Using Python I need to insert a newline character into a string every 64
Using Python I need to delete all characters in a multiline string up to
Ok so I need to download some web pages using Python and did a
I am using Python 3.1.1 on Mac OS X 10.6.2 and need an interface
I'm using Python with the NLTK toolkit in Apache via CGI. The toolkit need
I want to programmatically modify a bitmap using python but don't really need a
i need to check existing of Character (*,&,$) in Given String using python command
I am building a web application as college project (using Python), where I need

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.