[Tutor] passing results between functions
Gerhardus Geldenhuis
gerhardus.geldenhuis at gmail.com
Fri Apr 27 14:58:54 CEST 2012
Thank you for all the replies.
Having reread my question it is clear that it is not as well constructed as
it should be. I will have a bit more of a think with regards to what I want
to achieve and then if required rephrase my question.
Best Regards
On 25 April 2012 19:12, Alan Gauld <alan.gauld at btinternet.com> wrote:
> On 25/04/12 10:36, Gerhardus Geldenhuis wrote:
>
>> Hi
>> I wrote two functions which does different manipulations on text files.
>>
>> To start out with I passed the filename as a parameter and each function
>> opened the file and saved it.
>>
>
> I assume you mean it did some processing on the file data and then wrote
> it back?
>
>
> I then realized I would need to do that twice if I wanted to use both my
>> functions on the same file. I the modified the functions to take the
>> input as follows:
>> myfunction(open(sys.argv[1]),'**ro'))
>>
>
> You mean it took a file object and a string?
>
>
> It still wasn't good enough so I modified the function to return data as
>> follows:
>>
>> def myfunction
>> returndata = []
>> # some code
>> ...
>> return ''.join(returndata)
>>
>
> So it returns a string.
>
>
> so now I can do myfunction(mysecondfunction(**sys.argv[1],'ro'))
>> or mysecondfunction(myfunction(**sys.argv[1],'ro'))
>>
>
> Thats inconsistent since to one occasion you pass two arguments but on the
> other only one - the return value of the first function. And since the
> first parameter is expecting a file object the string will cause an error.
>
> But if you fixed the inconsistent data issue then the principle is fine.
>
>
> so my question is philosophical. Is that the pythonian way or is there a
>> better/easier/more efficient way to pass data?
>>
>
> It depends what kind of data and what you mean by "pass".
> You could use objects to pass more complex types of data, or tuples to
> pass multiple values. Or you could write the values into a shared database.
> It just depends on what you want to do, whether the function needs to e
> thread-safe, how big the data is, etc.
>
>
> To be honest I am still a bit stuck in how I did things when I
>> programmed in Delphi years ago and trying to make the paradigm shift and
>> understanding the data structures.
>>
>
> Thee is virtually no difference between Delphi and Python in the way
> functions (and objects) work. I'm not sure what paradigmn shift you have in
> mind. Delphi can't return tuples, but other than that the options and
> styles are pretty similar.
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> ______________________________**_________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
>
--
Gerhardus Geldenhuis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120427/eea97db4/attachment.html>
More information about the Tutor
mailing list