|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
||||
|
||||
|
return printed..
Ok, what I want to do is return any printed data from a function, or class, similar to Zope's "return printed" statment in Python scripts. I figured it might work just returning printed from the function but that obviously causes an error (global variable not defined).
Thanks alot guyz. Any help would be great! Mark. |
|
#2
|
|||
|
|||
|
I don't quite understand what you are trying to do. Could you elaborate a little more. (I think the problem is I'm not familiar with Zope)
|
|
#3
|
||||
|
||||
|
Ok
, nothing to do with Zope. Say you have a function which returns no value (prints instead) and you want to get this printed data as a return value so you can pass it to another function.In Zope, at the end of a Python script you need to include "return printed" or no output will be sent, the "printed" name must hold all the values printed by print, but I cant work out how to do this in Python. I could write a String buffer class and use it instead of print but I'd rather capture the values printed another way, i.e. but returning printed, but that doesnt seem to exist , maybe I'll suggest they make it possible in a latter version!Hope this is a little clearer. Mark. |
|
#4
|
|||
|
|||
|
I assume they just use a buffer for that, and then you return it and it prints whatever is returned in that buffer.
How else could you do it ? I doubt you can capture something that is printed to output ?? Cheers |
|
#5
|
||||
|
||||
|
Buffering
Yeah i kinda knew that
, was just looking for another more, elegant, way. I'd quite like to send stdout to stdin, but i can't work out how to read this back out into a variable simply because I dont know the in's and out of raw_input.Ive done it before by sending stdout to a file then reading it back in, but it seems a bit of a waist. I've also used a String Buffer but then this is what i was trying to avoid ![]() Any ideas? Mark. |
|
#6
|
|||
|
|||
|
you could just print the result of the function instead of printing within the function:
Code:
def doubler(x):
return 2 * x
print doubler(20)
make sense? |
|
#7
|
||||
|
||||
|
Mmmm..no?
LOL sorry, quite a comical responce dude.
Not what i really wanted to do, what i wanted to do was capture printed output or use a fuction which would store values as a string for use latter. The problem being that you can't pass a print stament to another fuction. Yes i could just so variable += 'string' but its not very elegant. I wrote a class which worked like this but opted for another way of doing it. I'm working on a templated content managment engine (kinda like zope but without the dynamic content, designed purly for content managment, portability and ease of use) and wanted to pass a functions output (printed) to re.sub. But I found a nicer way to do it using split. Thanks anyway, Mark. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > return printed.. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|