Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Python by (47.6k points)

Am I able to overload the print function and call the normal function from within? What I want to do is after a specific line I want print to call my print which will call the normal print and write a copy to file.

Also, I don't know how to overload print. I don't know how to do variable length arguments. I'll look it up soon but overload print python just told me I can't overload print in 2.x which is what I am using.

1 Answer

0 votes
by (106k points)

You can use the below-mentioned code for this:

class coder : 

def __init__(self, *coder) : 

self.coder = coder 

def write(self, text) : 

for w in self.coder : 

w.write(text) 

import sys 

saved = sys.stdout 

fout = file('out.log', 'w') 

sys.stdout = coder(sys.stdout, fout) 

print("Happy coding") 

sys.stdout = saved 

fout.close()

To know more about this you can have a look at the following video tutorial:-

 

Related questions

0 votes
2 answers
0 votes
1 answer
asked Sep 23, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Sep 23, 2019 in Python by Sammy (47.6k points)
+1 vote
2 answers
0 votes
4 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...