Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #75132 > unrolled thread

Re: one to many (passing variables)

Started byMartin S <shieldfire@gmail.com>
First post2014-07-24 09:27 +0200
Last post2014-07-24 09:15 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: one to many (passing variables) Martin S <shieldfire@gmail.com> - 2014-07-24 09:27 +0200
    Re: one to many (passing variables) Steven D'Aprano <steve@pearwood.info> - 2014-07-24 09:15 +0000

#75132 — Re: one to many (passing variables)

FromMartin S <shieldfire@gmail.com>
Date2014-07-24 09:27 +0200
SubjectRe: one to many (passing variables)
Message-ID<mailman.12271.1406186922.18130.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

Function A collects data and then calls function B with some, but also has data that should be passed to function C. 

But ofc if nested functions are allowed then that might solve the issue. I don't think I've seen nested functions mentioned in a tutorial I've been looking at. 

/martin s

On 24 Jul 2014, Chris Angelico <rosuav@gmail.com> wrote:
>On Thu, Jul 24, 2014 at 4:36 PM, Martin S <shieldfire@gmail.com> wrote:
>> How do you pass data from one function to many?
>>
>> I have functions A B and C. If data generated in A is useable in both
>> B and C how do I ensure this data is passed as needed? Or is it a
>> symptom of bad code?
>
>This is a little vague. Is there one function which calls A and then
>calls B and C? What's the relationship between them? Is it logical for
>A to itself call B and C? Are they all methods off one object?
>Top-level functions in a module?
>
>ChrisA

-- Sent with K-@ Mail - the evolution of emailing.

[toc] | [next] | [standalone]


#75134

FromSteven D'Aprano <steve@pearwood.info>
Date2014-07-24 09:15 +0000
Message-ID<53d0cec5$0$2814$c3e8da3$76491128@news.astraweb.com>
In reply to#75132
On Thu, 24 Jul 2014 09:27:10 +0200, Martin S wrote:

> Function A collects data and then calls function B with some, but also
> has data that should be passed to function C.

It might help if you give a bit more information. How does it collect 
data, how does it decide which bits of information should be passed to B 
and which to C, and what happens with the results returned from B and C?

But something like this should give you an idea:


def funca(values):
    data_for_b = []
    data_for_c = []
    for value in values:
        if 0 < value <= 100:
            data_for_b.append(value)
        elif 100 < value <= 200:
            data_for_c.append(value)
        # otherwise just discard it
    result_from_b = funcb(data_for_b)
    result_from_a = funcc(data_for_c)
    return max(result_from_b, result_from_a)


def funcb(values):
    return 5*sum(values)

def funcc(values):
    return 2*sum(values) - 200


print(funca([2, 5, 107, 99, 1999, 2345, 84, 156, 23]))


If you run that code, it should print 1065.


If this is not what you mean, I'm afraid you're going to have to explain 
what exactly you do mean, because I have no other ideas :-)


-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web