" p = [] " and "yield" statement


#1

<Below this line, add a link to the EXACT exercise that you are stuck at.>

not an exercice

<In what way does your code behave incorrectly? Include ALL error messages.>

it’s not incorrect

<What do you expect to happen instead?>
i dont know because i dont know what " [] " mean in python and that’s the question



def primes():
    yield 2
    n = 3
    p = []
    while True:
        # This works in Python 2.5+ 
        if not any(n % f == 0 for f in 
                     itertools.takewhile(lambda f: f*f <= n, p)): 
            yield n
            p.append(n)
        n += 2


So :

What allow / do the " [] " in this exemple ?

Another question what do the " yield " statemant ?

i read the following definition from wikipédia but don’t undestood well : “The yield statement, which returns a value from a generator function. From Python 2.5, yield is also an operator. This form is used to implement coroutines.


#2

[] is an operator just like + or -
It corresponds to a method named __getitem__
The general idea is to specify some key or index between the brackets, and it’ll return the corresponding value

yield is similar to return, except that the function (actually the yield makes it a generator, not a function) can resume afterwards, provided that the caller continues to ask for more values

yield can also be used in some clever ways like for resource management. For example you could open a file, yield the file object, and then close it after the yield (but you would have to use this in an appropriate manner to take advantage of it)


#3
  • yield statement is a return the break in less ?
  • yield statement return a value but the SubProg continue ?
  • return statement is a yield + break ?

#4

No, a function call isn’t a sub-process
And break doesn’t exit a function


#5

yield is really well explained here:


#6

For that i think the are the with statement :

" The with statement (from Python 2.5), which encloses a code block within a context manager (for example, acquiring a lock before the block of code is run and releasing the lock afterwards, or opening a file and then closing it), allowing Resource Acquisition Is Initialization (RAII)-like behavior. " wikipedia


#7

i will check that afterwards i return here give my understanding of the article your share me :wink:


#8

Yes. With-statements are used to open/enter and close/exit context managers. One way to create a context manager is with a generator which yields the resource, and then decorate it with contextlib.contextmanager.


#9
from contextlib import contextmanager

@contextmanager
def SomethingShiny():
    print('created resource')
    yield 'absolutely nothing'
    print('closed resource')


with SomethingShiny() as resource:
    print('doing stuff with the resource..')
    print(''.join(reversed(resource)))
    print('finished doing stuff with the resource')

output:

created resource                                                               
doing stuff with the resource..
gnihton yletulosba
finished doing stuff with the resource
closed resource

But more commonly a generator will be used to produce one value at a time, without computing everything up-front. (This is called lazy evaluation)


#10

“yield is a keyword that is used like return, except the function will return a generator.”

so :slight_smile: (i %$@#? the smiley automation … i want just write " : " …) -> so :

return statemeant return a Value and the yield statemant return a generator .

Right ?

i will continue to read the article see you later. (i will eat : 12:36 here)


#11

Yes, a generator function will immediately return without doing anything. It returns a generator. It’s only when you start asking values from the generator that the code actually executes.


#12

Can i just “Like” your post for saying : “i read you” ?

or

The “liking button” is only for saying : “i fuking love what your said / share / learn me” ?


#13

doesn’t matter


#14

The initial topic was on this XD

Can i change the name of my topic ?

or

can you said me more about the “[]” because i dont entierely get it :confused:

p.s : i go eat i wil be back soon (av. 20 min)


#15

Pretty sure there’s a field for the topic if you click “edit” on your first post in the thread.

I don’t think there’s much to say about [], it is indeed very similar to +/-
But if I missed something, ask something more specific


#16

np

you can set up the topic as “clear” / “answered” :wink:


#17

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.