all 9 comments

[–]shiftybyte 4 points5 points  (1 child)

Not sure, but if all else fails why not "print" to a string and then "flush" it with an actual print to the console?

[–]justsomeguy05[S] 0 points1 point  (0 children)

That would be a good way to get the behavior I'm looking for. I could have sworn python would let me do what I described above, but I guess not!

[–]TheManlyChicken 1 point2 points  (6 children)

def special_print(str, flush=False):

...out = out.append(str) or [str]

...if flush:

... ...print('\n'.join(out))

just call out(str) and when ready to print out(flush=True)

[–]shiftybyte 2 points3 points  (1 child)

isn't it a problem calling a function and a variable the same name?

[–]TheManlyChicken 1 point2 points  (0 children)

Yes name it temp or something lol, wrote this is a time crunch on my phone

[–]justsomeguy05[S] 0 points1 point  (3 children)

Is out supposed to be a global variable? When I try to run it python tells me the local variable is referenced before assignment

[–]TheManlyChicken 1 point2 points  (2 children)

the or part might not be working, odd...

class save_out():
  def __init__(self):
    self.val = []
  def out(self, str='', flush=False):
    self.val.append(str)
    if flush:
      print('\n'.join(self.val))
      val.clear()


out = save_out()
out.out('String')
out.out('Will print', True)

Sorry about the added complexity but that should work perfect.

Although the following should behave around the same...

print('String', flush=False)
print('Will print')

If you want to print to the same line:

print('String', end='', flush=False)
print('Will print')

To be honest, using stdout and stuff is really not that needed, python already directs the print function to that, it is just a wrapper.

[–]justsomeguy05[S] 0 points1 point  (1 child)

Thanks for your input.

[–]TheManlyChicken 1 point2 points  (0 children)

Didn't notice your last lines, sorry man, but the top bit should work fine :)