How to keep a function as a generator function when the yield operatoris moved into its sub-function

Discussion in 'Python' started by weafon, Jul 14, 2009.

  1. weafon

    weafon Guest

    Hi guys,

    I have a question about the usage of yield. As shown in the below
    example, in general, if there is a code segment commonly used by two or
    more functions, we may isolate the segment into a function and then call
    it from other functions if necessary.

    def func1():
    ....
    while(cond):
    .....
    commoncode()
    ...


    def func2():
    ....
    while(cond):
    .....
    commoncode()
    ...

    def commoncode()
    AAAA
    BBBB
    CCCC

    However, if there is a 'yield' operation in the common code segment, the
    isolation causes that func1 and func2 become a non-generator function!!
    Although I can prevent such an isolation by just duplicating the segment
    in func1 and func2 to keep both of them being generator functions, the
    code may become ugly and hard to maintain particularly when coomoncode()
    is long.

    The problem may be resolved if I can define the commoncode() as an
    inline function or marco. Unfortunately, inline and marco do not seems
    to be implemented in python. Thus, how can I isolate a common segment
    into a function when there are yield operations in the common segment?

    Thanks,
    Weafon
     
    weafon, Jul 14, 2009
    #1
    1. Advertisements

  2. Re: How to keep a function as a generator function when the yieldoperator is moved into its sub-functions??

    weafon schrieb:
    > Hi guys,
    >
    > I have a question about the usage of yield. As shown in the below
    > example, in general, if there is a code segment commonly used by two or
    > more functions, we may isolate the segment into a function and then call
    > it from other functions if necessary.
    >
    > def func1():
    > ....
    > while(cond):
    > .....
    > commoncode()
    > ...
    >
    >
    > def func2():
    > ....
    > while(cond):
    > .....
    > commoncode()
    > ...
    >
    > def commoncode()
    > AAAA
    > BBBB
    > CCCC
    >
    > However, if there is a 'yield' operation in the common code segment, the
    > isolation causes that func1 and func2 become a non-generator function!!



    No. Not writing them as generators makes them a non-generator-function.

    You are way to unspecific with your examples. But if func1 and func2 are
    themselves supposed to be generators, there is nothing preventing you
    from using them as such.

    Diez
     
    Diez B. Roggisch, Jul 14, 2009
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben
    Replies:
    2
    Views:
    1,071
  2. Replies:
    1
    Views:
    395
    Gabriel Genellina
    Apr 22, 2008
  3. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,445
    Lawrence D'Oliveiro
    May 20, 2011
  4. Markus
    Replies:
    1
    Views:
    281
    Mark Hubbart
    Sep 27, 2004
  5. Michael Edgar
    Replies:
    13
    Views:
    364
    Brian Candler
    Apr 21, 2011
Loading...

Share This Page