#1
  1. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69

    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. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    6
    Rep Power
    0
    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)
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69

    Talking


    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.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    325
    Rep Power
    12
    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
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69

    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.
  10. #6
  11. No Profile Picture
    Tattooed Python-Lovin' Freak-Boy
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    orange county, CA
    Posts
    16
    Rep Power
    0
    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?
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69

    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.

IMN logo majestic logo threadwatch logo seochat tools logo