• Comprehensive Learning Paths
  • 150+ Hours of Videos
  • Complete Access to Jupyter notebooks, Datasets, References.

Rating

Python Yield – What does the yield keyword do?

  • September 7, 2020
  • Venmani A D

Adding yield keyword to a function will make the function return a generator object that can be iterated upon.

What does the yield keyword do?

Approaches to overcome generator exhaustion, how to materialize generators, how yield works, step by step, exercise 1: write a program to create a generator that generates cubes of numbers up to 1000 using yield, exercise 2: write a program to return odd number by pipelining generators, difference between yield and return.

yield in Python can be used like the return statement in a function. When done so, the function instead of returning the output, it returns a generator that can be iterated upon.

You can then iterate through the generator to extract items. Iterating is done using a for loop or simply using the next() function. But what exactly happens when you use yield ?

What the yield keyword does is as follows:

Each time you iterate, Python runs the code until it encounters a yield statement inside the function. Then, it sends the yielded value and pauses the function in that state without exiting.

When the function is invoked the next time, the state at which it was last paused is remembered and execution is continued from that point onwards. This continues until the generator is exhausted.

What does remembering the state mean?

It means, any local variable you may have created inside the function before yield was called will be available the next time you invoke the function. This is NOT the way a regular function usually behaves.

Now, how is it different from using the return keyword?

Had you used return in place of yield , the function would have returned the respective value, all the local variable values that the function had earlier computed would be cleared off and the next time the function is called, the function execution will start fresh.

Since the yield enables the function to remember its ‘state’, this function can be used to generate values in a logic defined by you. So, it function becomes a ‘generator’.

Now you can iterate through the generator object. But it works only once.

Calling the generator the second time wont give anything. Because the generator object is already exhausted and has to be re-initialized.

yield in assignment python

If you call next() over this iterator, a StopIteration error is raised

To overcome generator exhaustion, you can:

  • Approach 1 : Replenish the generator by recreating it again and iterate over. You just saw how to do this.
  • Approach 2 : Iterate by calling the function that created the generator in the first place
  • Approach 3 (best) : Convert it to an class that implements a __iter__() method. This creates an iterator every time, so you don’t have to worry about the generator getting exhausted.

We’ve see the first approach already. Approach 2: The second approach is to simple replace the generator with a call the the function that produced the generator, which is simple_generator() in this case. This will continue to work no matter how many times you iterate it.

Approach 3: Now, let’s try creating a class that implements a __iter__() method. It creates an iterator object every time, so you don’t have to keep recreating the generator.

We often store data in a list if you want to materialize it at some point. If you do so, the content of the list occupies tangible memory. The larger the list gets, it occupies more memory resource.

But if there is a certain logic behind producing the items that you want, you don’t have to store in a list. But rather, simply write a generator that will produce the items whenever you want them.

Let’s say, you want to iterate through squares of numbers from 1 to 10. There are at least two ways you can go about it: create the list beforehand and iterate. Or create a generator that will produce these numbers.

Let’s do the same with generators now.

Generators are memory efficient because the values are not materialized until called. And are usually faster. You will want to use a generator especially if you know the logic to produce the next number (or any object) that you want to generate.

Can a generator be materialized to a list?

Yes. You can do so easily using list comprehensions or by simply calling list() .

yield is a keyword that returns from the function without destroying the state of it’s local variables. When you replace return with yield in a function, it causes the function to hand back a generator object to its caller. In effect, yield will prevent the function from exiting, until the next time next() is called. When called, it will start executing from the point where it paused before. Output:

See that I have created a function using yield keyword. Let’s try to access the function, as we have created an object obj for the function, it will be defined as an iterator. So to access it, use the next() function. It will iterate until the next yield statement is reached.

explaining yield function

I am going to try to create a generator function which will return the cubic of the number until the cube limit reaches 1000, one at a time using yield keyword. The memory will be alloted only to the element which is running, after the execution of output of that element, the memory will be deleted.

Multiple generators can be pipelined(one generator using another) as a series of operations in the same code. Pipelining also makes the code more efficient and easy to read. For pipeling functions, use () paranthesis to give function caller inside a function.

More Articles

How to convert python code to cython (and speed up 100x), how to convert python to cython inside jupyter notebooks, install opencv python – a comprehensive guide to installing “opencv-python”, install pip mac – how to install pip in macos: a comprehensive guide, scrapy vs. beautiful soup: which is better for web scraping, add python to path – how to add python to the path environment variable in windows, similar articles, complete introduction to linear regression in r, how to implement common statistical significance tests and find the p value, logistic regression – a complete tutorial with examples in r.

Subscribe to Machine Learning Plus for high value data science content

© Machinelearningplus. All rights reserved.

yield in assignment python

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free sample videos:.

yield in assignment python

Datagy logo

  • Learn Python
  • Python Lists
  • Python Dictionaries
  • Python Strings
  • Python Functions
  • Learn Pandas & NumPy
  • Pandas Tutorials
  • Numpy Tutorials
  • Learn Data Visualization
  • Python Seaborn
  • Python Matplotlib

Using Python Generators and yield: A Complete Guide

  • February 15, 2023 February 15, 2023

Using Python Generators and yield A Complete Guide Cover Image

In this tutorial, you’ll learn how to use generators in Python, including how to interpret the yield expression and how to use generator expressions . You’ll learn what the benefits of Python generators are and why they’re often referred to as lazy iteration. Then, you’ll learn how they work and how they’re different from normal functions.

Python generators provide you with the means to create your own iterator functions. These functions allow you to generate complex, memory-intensive operations. These operations will be executed lazily, meaning that you can better manage the memory of your Python program.

By the end of this tutorial, you’ll have learned:

  • What Python generators are and how to use the yield expression
  • How to use multiple yield keywords in a single generator
  • How to use generator expressions to make generators simpler to write
  • Some common use cases for Python generators

Table of Contents

Understanding Python Generators

Before diving into what generators are, let’s explore what iterators are. Iterators are objects that can be iterated upon, meaning that they return one action or item at a time . To be considered an iterator, objects need to implement two methods: __iter__() and __next__() . Some common examples of iterators in Python include for loops and list comprehensions .

Generators are a Pythonic implementation of creating iterators, without needing to explicitly implement a class with __iter__() and __next__() methods. Similarly, you don’t need to keep track of the object’s internal state. An important thing to note is that generators iterate over an object lazily, meaning they do not store their contents in memory .

The yield statement’s job is to control the flow of a generator function. The statement goes further to handle the state of the generator function, pausing it until it’s called again, using the next() function.

Creating a Simple Generator

In this section, you’ll learn how to create a basic generator. One of the key syntactical differences between a normal function and a generator function is that the generator function includes a yield statement .

Let’s see how we can create a simple generator function:

Let’s break down what is happening here:

  • We define a function, return_n_values() , which takes a single parameter, n
  • In the function, we first set the value of num to 0
  • We then enter a while loop that evaluates whether the value of num is less than our function argument, n
  • While that condition is True , we yield the value of num
  • Then, we increment the value of num using the augmented assignment operator

Immediately, there are two very interesting things that happen:

  • We use yield instead of return
  • A statement follows the yield statement, which isn’t ignored

Let’s see how we can actually use this function:

In the code above, we create a variable values , which is the result of calling our generator function with an argument of 5 passed in. When we print the value of values , a generator object is returned.

So, how do we access the values in our generator object? This is done using the next() function, which calls the internal .__iter__() method. Let’s see how this works in Python:

We can see here that the value of 0 is returned. However, intuitively, we know that the values of 0 through 4 should be returned. Because a Python generator remembers the function’s state, we can call the next() function multiple times . Let’s call it a few more times:

In this case, we’ve yielded all of the values that the while loop will accept. Let’s see what happens when we call the next() function a sixth time:

We can see in the code sample above that when the condition of our while loop is no longer True , Python will raise StopIteration .

In the next section, you’ll learn how to create a Python generator using a for loop.

Creating a Python Generator with a For Loop

In the previous example, you learned how to create and use a simple generator. However, the example above is complicated by the fact that we’re yielding a value and then incrementing it. This can often make generators much more difficult for beginners and novices to understand.

Instead, we can use a for loop, rather than a while loop, for simpler generators . Let’s rewrite our previous generator using a for loop to make the process a little more intuitive:

In the code block above, we used a for loop instead of a while loop. We used the Python range() function to create a range of values from 0 through to the end of the values. This simplifies the generator a little bit, making it more approachable to readers of your code.

Unpacking a Generator with a For Loop

In many cases, you’ll see generators wrapped inside of for loops, in order to exhaust all possible yields. In these cases, the benefit of generators is less about remembering the state (though this is used, of course, internally), and more about using memory wisely.

In the code block above, we used a for loop to loop over each iteration of the generator. This implicitly calls the __next__() method. Note that we’re using the optional end= parameter of the print function, which allows you to overwrite the default newline character .

Creating a Python Generator with Multiple Yield Statements

A very interesting difference between Python functions and generators is that a generator can actually hold more than one yield expressions ! While, theoretically, a function can have more than one return keyword, nothing after the first will execute.

Let’s take a look at an example where we define a generator with more than one yield statement:

In the code block above, our generator has more than one yield statement. When we call the first next() function, it returns only the first yielded value. We can keep calling the next() function until all the yielded values are depleted. At this point, the generator will raise a StopIteration exception.

Understanding the Performance of Python Generators

One of the key things to understand is why you’d want to use a Python generator. Because Python generators evaluate lazily, they use significantly less memory than other objects.

For example, if we created a generator that yielded the first one million numbers, the generator doesn’t actually hold the values. Meanwhile, by using a list comprehension to create a list of the first one million values, the list actually holds the values. Let’s see what this looks like:

In the code block above, we import the sys library which allows us to access the getsizeof() function. We then print the size of both the generator and the list. We can see that the list is over 75,000 times larger.

In the following section, you’ll learn how to simplify creating generators by using generator expressions.

Creating Python Generator Expressions

When you want to create one-off generators, using a function can seem redundant. Similar to list and dictionary comprehensions , Python allows you to create generator expressions. This simplifies the process of creating generators, especially for generators that you only need to use once.

In order to create a generator expression, you wrap the expression in parentheses . Say you wanted to create a generator that yields the numbers from zero through four. Then, you could write (i for i in range(5)) .

In the example above, we used a generator expression to yield values from 0 to 4. We then call the next() function five times to print out the values in the generator.

In the following section, we’ll dive further into the yield statement.

Understanding the Python yield Statement

The Python yield statement can often feel unintuitive to newcomers to generators. What separates the yield statement from the return statement is that rather than ending the process, it simply suspends the current process.

The yield statement will suspend the process and return the yielded value. When the subsequent next() function is called, the process is resumed until the following value is yielded.

What is great about this is that the state of the process is saved. This means that Python will know where to pick up its iteration, allowing it to move forward without a problem.

How to Throw Exceptions in Python Generators Using throw

Python generators have access to a special method, .throw() , which allows them to throw an exception at a specific point of iteration . This can be helpful if you know that an erroneous value may exist in the generator.

Let’s take a look at how we can use the .throw() method in a Python generator:

Let’s break down how we can use the .throw() method to throw an exception in a Python generator:

  • We create our generator using a generator expression
  • We then use a for loop to loop over each value
  • Within the for loop, we use an if statement to check if the value is equal to 3. If it is, we call the .throw() method, which raises an error

In some cases, you may simply want to stop a generator, rather than throwing an exception. This is what you’ll learn in the following section.

How to Stop a Python Generator Using stop

Python allows you to stop iterating over a generator by using the .close() function . This can be very helpful if you’re reading a file using a generator and you only want to read the file until a certain condition is met.

Let’s repeat our previous example, though we’ll stop the generator rather than throwing an exception:

In the code block above we used the .close() method to stop the iteration. While the example above is simple, it can be extended quite a lot. Imagine reading a file using Python – rather than reading the entire file, you may only want to read it until you find a given line.

In this tutorial, you learned how to use generators in Python, including how to interpret the yield expression and how to use generator expressions . You learned what the benefits of Python generators are and why they’re often referred to as lazy iteration. Then, you learned how they work and how they’re different from normal functions.

Additional Resources

To learn more about related topics, check out the resources below:

  • Understanding and Using Functions in Python for Data Science
  • Python: Return Multiple Values from a Function
  • Python Built-In Functions
  • Python generators: Official Documentation

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials. View Author posts

2 thoughts on “Using Python Generators and yield: A Complete Guide”

' src=

Guides like these are highlights in the net. Thanks

' src=

Thanks so much, Vik!!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Save my name, email, and website in this browser for the next time I comment.

  • Introduction to Python (for experienced coders)
  • Advanced Python
  • Python for non-programmers
  • Testing Python code with pytest
  • Data analytics with NumPy and Pandas
  • Introduction to machine learning in Python
  • Python for system administrators
  • Python Practice Workshop
  • Regular expressions
  • Introduction to Git
  • Ace Python Interviews (free)
  • Intro Python: Fundamentals
  • Intro Python: Functions
  • Object-oriented Python
  • Advanced python functions
  • Advanced Python data structures
  • Advanced Python objects
  • Comprehending Comprehensions
  • Weekly Python Exercise
  • Understanding and mastering Git
  • Practice Makes Regexp
  • Python Workout
  • Pandas Workout
  • Regular expressions crash course
  • Boolean indexing in NumPy and Pandas
  • Variable scoping in Python
  • Working with files in Python
  • Teach programming better
  • About Reuven

Making sense of generators, coroutines, and “yield from” in Python

May 8, 2020 . By Reuven

Consider the following (ridiculous) Python function:

I can define the function, and then run it. What do I get back?

Not surprisingly, I get 1 back. That’s because Python reaches that first “return” statement and returns 1. There’s no need to continue onto the second and third “return” statements. (Actually, from Python’s perspective, those latter two statements don’t even exist; they are removed from the bytecode altogether at compilation time.)

What happens if I write my function a bit differently, using “ yield ” instead of “ return “?

If I run my function now, I get the following:

That’s right: Because I used “yield” instead of “return”, running the function doesn’t execute the function body. Rather, I get back a generator object, meaning something that implements the iterator protocol. For this reason, the second kind of function (using “yield”) is called a “generator function,” although you’ll often hear people describe them as “generators.”

Because generators (i.e., the objects returned by generator functions) implement the iterator protocol, they can be put into “for” loops:

What do we get back?

How does this work? With each iteration, the body of the generator function is executed. If there’s a “yield” statement, then that value is returned to the “for” loop. And then, most significantly, the generator goes to sleep, pausing immediately after that “yield” statement executes. When the next iteration occurs, the function wakes up at the point where it was paused, and continues running, as if nothing at all had happened.

In other words:

  • A generator function, when executed, returns a generator object.
  • The generator object implements the iterator protocol, meaning that it knows what to do in a “for” loop.
  • Each time the generator reaches a “yield” statement, it returns the yielded value to the “for” loop, and goes to sleep.
  • With each successive iteration, the generator starts running from where it paused (i.e., just after the most recent “yield” statement)
  • When the generator reaches the end of the function, or encounters a “return” statement, it raises a StopIteration exception, which is how Python iterators indicate that they’ve reached the end of the line.

We can simulate this all ourselves, as follows:

The “ next ” built-in function is how Python asks an iterator for … well, for the next object that it wants to produce. The response to “next” can either be an object or the StopIteration exception.

This kind of generator function can be quite useful: You can use it for caching, filtering, and treating infinite (or very large) data sets in smaller chunks.

But used in this way, generator functions are for one-way communication. We can retrieve information from a generator using “next”, but we cannot interact with it, modify its trajectory, or otherwise affect its execution while it’s running. (That’s not entirely true: The “ throw ” method allows you to force an exception to be raised within the generator, which you can use to affect what the generator should do.)

A number of years ago, Python introduced the “send” method for generators, and a modification of how “yield” can be used. Consider this code:

The above code looks a bit weird, in that “yield’ is on the right side of an assignment statement. This means that “yield” must be providing a value to the generator. Where is it getting that value from?

Answer: From the “send” method, which can be invoked in place of the “next” function. The “send” method works just like “next”, except that you can pass any Python data structure you want into the generator. And whatever you send to the generator is then assigned to “x”.

Now, you need to “prime” it the first time with “next”, rather than “send”. But other than that, it works just like any other generator — except that whatever you “send” will then be a part of the coroutine. As before, each invocation of next/send will execute all of the code until and including the “yield” statement.

It might seem weird, but because the “yield” is on the right side of an assignment operator, and because the right side of assignment always executes before the left side, the generator goes to sleep after returning the right side, but before assigning any value to the left side. When it wakes up, the first thing that happens is that the sent value is assigned to the left side.

Here’s how that can look:

Now, this is admittedly pretty neat: Our coroutine hangs around, waiting for us to give it a number to dial.

For a long time, it seemed like such coroutines were solutions looking for problems. After all, what can you do with such a thing? From what I can tell, people in the Python world were excited about this sort of idea, but aside from a handful who really understood the potential, coroutines were ignored and seen as somewhere between weird and esoteric.

(I should add that the term “coroutine” has changed ts meaning somewhat in the Python world over the last few years, as the “ asyncio ” library has gained in popularity. I have nothing against asyncio, and have been increasingly impressed with what it does, and how it does it. But that’s not the sort of coroutine I’m talking about here. Note that asyncio’s coroutines started off as generators, and there are still many things to understand in asyncio via generators. But that’s not my topic here.)

So, where do you use a generator-based coroutine? How can you think about it?

My suggestion: Think of it as an in-program microservice. A nanoservice, if you will, available to your Python program.

Why do I say this? Because the moment that you think of it this way, what you do and don’t want to do with coroutines becomes much clearer.

  • Want to communicate with a database? Use a coroutine, whose local variables will stick around across queries, and can thus remain connected without using lots of ugly global variables. Send your SQL queries to the coroutine, and get back the query results.
  • Want to communicate with an external network service, such as a stock-market quote system? Use a coroutine, to which you can send a tuple of symbol and date, and from which you’ll receive the latest information in a dictionary.
  • Want to automatically translate files from one format to another? Use a coroutine, which can take input in one encoding/format and produce output in another encoding/format.

Let’s create two simple coroutines that demonstrate how this can work. First, a Pig Latin translator, which will receive strings in English and will return them translated into Pig Latin:

Our service coroutine is “pig_latin_translator”, which uses the “pl_sentence” function to do its translation work. Let’s fire it up:

Amazing! Whenever we want to translate some English into Pig Latin, we can do so with our translator, sitting in memory and waiting to serve us. Perhaps this isn’t the most elegant or sophisticated use of coroutines, but it certainly works.

Let’s look at another example: A corporate support chatbot. You know, the sort of thing that appears on a company’s Web site, allows you to enter your complaints, and then actually helps you. No, wait — that’s science fiction; in reality, such chat bots are always unable to help, while telling you how important you are. Let’s create such an unhelpful chatbot:

This chatbot, as its name implies, waits for your input, and then ignores it entirely, returning a canned message meant to make you feel good about yourself and the service you’re getting. Of course, you don’t really feel good after such a conversation, but at least the company has saved on salaries, right?

But I digress.

Let’s see what happens when we run our chatbot:

A number of years ago, Python introduced a new form of “yield”, known as “ yield from “. And I have to say that the documentation and examples are … well, they make the simple case very obvious and easy to understand, but make the hard case quite difficult to understand. I hope that I can clear that up.

The basic idea is that if you have a function, it’s normal to call other functions from within it. That’s a standard technique in programming, one which allows us to write shorter, more specific functions, as well as to take advantage of abstraction.

But what if you have a generator that wants to return data from another generator, or any other iterable? You could do something like this:

In other words, we turn to “g”, our generator. And with each iteration, we ask it for its next element. What does the generator do? It invokes a “for” loop on “data”. So with each iteration, we’re asking “g”, and “g” is asking “data”. We can shorten this code with “yield from”:

We got the same result, even though the body of “wrapper” is now dramatically shorter. “yield from” basically lets us outsource the “yield” to another iterable, namely “data”. Our generator is basically saying, “I don’t want to deal with this any more, so I’ll just ask data to take over from here.”

This is the simple use case for “yield from”, and it’s not really very compelling. After all, did they need to add new syntax to the language in order to reduce our “for” loops? The answer is “no.”

So what is “yield from” used for? Consider the two coroutines that we wrote above, for Pig Latin and customer service. Imagine that the companies providing these services have now merged, and that we would like to have a single in-memory service that handles both of them. In other words, we would like to have a coroutine to which we can send “1” to translate Pig Latin and “2” to get customer service.

This all sounds fine, until we realize that we’re somehow going to need to get a value from the caller’s “send” method, and then pass it along to one of our coroutines. That’s going to look rather messy, no?

And so, the real reason to use “yield from” is when you have a coroutine that acts as an agent between its caller and other coroutines. By using “yield from”, you not only outsource the yielded values to a sub-generator, but you also allow that sub-generator to get inputs from the user. For example:

Now, what happens if we invoke this?

Fantastic, right? We’re calling “s.send” — meaning, our messages are being sent to the switchboard coroutine. But because it has used “yield from”, our message is passed along to “pig_latin_translator”. And when the translation is done, that coroutine yields its value, which bubbles up directly to the original caller.

Of course, I can also get customer support:

Pretty nifty, eh? But we can do even better, allowing people to go back from our sub-generator to our main one, and then choose a different one:

Here’s an example of how that would work:

So, what have we seen here?

  • Coroutines are like in-memory microservices, with state that remains across calls.
  • We use “next” to prime a coroutine the first time, and then use “send” to deliver additional messages.
  • If we want to provide a meta-microservice, or a coroutine that invokes other coroutines, then we can use “yield from”.
  • “yield from” connects the initial “send” method with the sub-coroutine, effectively passing through the coroutine that’s using “yield from”.

I hope that this helps you to consider when and how to use coroutines — and also how you can use “yield from” in your code in more sophisticated ways than just avoiding “for” loops.

Related Posts

Prepare yourself for a better career, with my new Python learning memberships

I’m banned for life from advertising on meta. because i teach python., sharpen your pandas skills with “bamboo weekly”.

This article is very nice and well written. Yet I have a question: a function which send requests to a database could be a coroutine, or the method of a class. In both cases, connection parameters could be stored, and the class or the coroutine would sit in memory waiting for requests. The advantage of the coroutine remains unclear to me, where is the asynchronicity here? will the code behave somehow differently? be faster? better handle blocking IO? if I use a coroutine vs a method?

It’s mostly a matter of packaging and style. Objects give you a higher-level abstraction. Closures are probably a bit faster (but not much, necessarily), and keep things a bit more private.

To be honest, I usually think of this kind of coroutine as (a) an interesting intellectual exercise or (b) a way to start understanding the mindset of asyncio, which uses a different type of coroutine, but is still in the same direction.

That said, I recently spoke with people who are using precisely this kind of architecture for Web applications in Rust (not in Python). I was fascinated to hear what they had to say about it, and was a bit surprised to hear that it was really in use as an architecture!

For the most part, generator-based coroutines in Python are no longer really used.

This is amazing post!!! It helped clarified my understanding on coroutines and yield from. Thank you!

In the last example when you send None what is going on?

He is breaking from the latin translator (there is a if is is None: break;) and so the main message (menu) is displayed

Thank You very much. I really learned something new.

Thank you very much! This is just what needed!

I learned something new. Thank you!

Very helpful article, thanks. (Found it through a google search for `yield from`). FWIW, I noticed one typo: in your second generation switchboard example, you show the prompt after the switchboard assignment instead of after the initial `next()`.

Thanks, and I’ll fix the typo!

I also found this article through searching `yield from`. It was very confusing to me, but this article for sure helped me so much!

yield in assignment python

Session expired

Please log in again. The login page will open in a new tab. After logging in you can close it and return to this page.

Understanding Python's "yield" Keyword

yield in assignment python

The yield keyword in Python is used to create generators. A generator is a type of collection that produces items on-the-fly and can only be iterated once. By using generators you can improve your application's performance and consume less memory as compared to normal collections, so it provides a nice boost in performance.

In this article we'll explain how to use the yield keyword in Python and what it does exactly. But first, let's study the difference between a simple list collection and generator, and then we will see how yield can be used to create more complex generators.

  • Differences Between a List and Generator

In the following script we will create both a list and a generator and will try to see where they differ. First we'll create a simple list and check its type:

When running this code you should see that the type displayed will be "list".

Now let's iterate over all the items in the squared_list .

The above script will produce following results:

Now let's create a generator and perform the same exact task:

To create a generator, you start exactly as you would with list comprehension, but instead you have to use parentheses instead of square brackets. The above script will display "generator" as the type for squared_gen variable. Now let's iterate over the generator using a for-loop.

The output will be:

The output is the same as that of the list. So what is the difference? One of the main differences lies in the way the list and generators store elements in the memory. Lists store all of the elements in memory at once, whereas generators "create" each item on-the-fly, displays it, and then moves to the next element, discarding the previous element from the memory.

One way to verify this is to check the length of both the list and generator that we just created. The len(squared_list) will return 5 while len(squared_gen) will throw an error that a generator has no length. Also, you can iterate over a list as many times as you want but you can iterate over a generator only once. To iterate again, you must create the generator again.

  • Using the Yield Keyword

Now we know the difference between simple collections and generators, let us see how yield can help us define a generator.

In the previous examples, we created a generator implicitly using the list comprehension style. However in more complex scenarios we can instead create functions that return a generator. The yield keyword, unlike the return statement, is used to turn a regular Python function in to a generator. This is used as an alternative to returning an entire list at once. This will be again explained with the help of some simple examples.

Again, let's first see what our function returns if we do not use the yield keyword. Execute the following script:

In this script a function cube_numbers is created that accepts a list of numbers, take their cubes and returns the entire list to the caller. When this function is called, a list of cubes is returned and stored in the cubes variable. You can see from the output that the returned data is in-fact a full list:

Now, instead of returning a list, let's modify the above script so that it returns a generator.

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

In the above script, the cube_numbers function returns a generator instead of list of cubed number. It's very simple to create a generator using the yield keyword. Here we do not need the temporary cube_list variable to store cubed number, so even our cube_numbers method is simpler. Also, no return statement is needed, but instead the yield keyword is used to return the cubed number inside of the for-loop.

Now, when cube_number function is called, a generator is returned, which we can verify by running the code:

Even though we called the cube_numbers function, it doesn't actually execute at this point in time, and there are not yet any items stored in memory.

To get the function to execute, and therefore the next item from generator, we use the built-in next method. When you call the next iterator on the generator for the first time, the function is executed until the yield keyword is encountered. Once yield is found the value passed to it is returned to the calling function and the generator function is paused in its current state.

Here is how you get a value from your generator:

The above function will return "1". Now when you call next again on the generator, the cube_numbers function will resume executing from where it stopped previously at yield . The function will continue to execute until it finds yield again. The next function will keep returning cubed value one by one until all the values in the list are iterated.

Once all the values are iterated the next function throws a StopIteration exception. It is important to mention that the cubes generator doesn't store any of these items in memory, rather the cubed values are computed at runtime, returned, and forgotten. The only extra memory used is the state data for the generator itself, which is usually much less than a large list. This makes generators ideal for memory-intensive tasks.

Instead of always having to use the next iterator, you can instead use a "for" loop to iterate over a generators values. When using a "for" loop, behind the scenes the next iterator is called until all the items in the generator are iterated over.

  • Optimized Performance

As mentioned earlier, generators are very handy when it comes to memory-intensive tasks since they do not need to store all of the collection items in memory, rather they generate items on the fly and discards it as soon as the iterator moves to the next item.

In the previous examples the performance difference of a simple list and generator was not visible since the list sizes were so small. In this section we'll check out some examples where we can distinguish between the performance of lists and generators.

In the code below we will write a function that returns a list that contains 1 million dummy car objects. We will calculate the memory occupied by the process before and after calling the function (which creates the list).

Take a look at the following code:

Note : You may have to pip install psutil to get this code to work on your machine.

In the machine on which the code was run, following results were obtained (yours may look slightly different):

Before the list was created the process memory was 8 MB , and after the creation of list with 1 million items, the occupied memory jumped to 334 MB . Also, the time it took to create the list was 1.58 seconds.

Now, let's repeat the above process but replace the list with generator. Execute the following script:

Here we have to use the for car in car_list_gen(1000000) loop to ensure that all 1000000 cars are actually generated.

Following results were obtained by executing the above script:

From the output, you can see that by using generators the memory difference is much smaller than before (from 8 MB to 40 MB ) since the generators do not store the items in memory. Furthermore, the time taken to call the generator function was a bit faster as well at 1.37 seconds, which is about 14% faster than the list creation.

Hopefully from this article you have a better understanding of the yield keyword, including how it's used, what it's used for, and why you'd want to use it. Python generators are a great way to improve the performance of your programs and they're very simple to use, but understanding when to use them is the challenge for many novice programmers.

You might also like...

  • Hidden Features of Python
  • Python Docstrings
  • Handling Unix Signals in Python
  • The Best Machine Learning Libraries in Python
  • Guide to Sending HTTP Requests in Python with urllib3

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

In this article

yield in assignment python

Building Your First Convolutional Neural Network With Keras

Most resources start with pristine datasets, start at importing and finish at validation. There's much more to know. Why was a class predicted? Where was...

David Landup

Data Visualization in Python with Matplotlib and Pandas

Data Visualization in Python with Matplotlib and Pandas is a course designed to take absolute beginners to Pandas and Matplotlib, with basic Python knowledge, and...

© 2013- 2024 Stack Abuse. All rights reserved.

Popular Articles

  • Syntax And Usage (May 23, 2024)
  • Reading And Writing (Apr 27, 2024)
  • Asyncio (Apr 26, 2024)
  • Metaclasses (May 04, 2024)
  • Type Hints (May 04, 2024)

python yield

Switch to English

Table of Contents

Introduction

Understanding generators, explanation of 'yield from', tips and tricks, common error-prone cases and how to avoid them.

  • To fully appreciate the 'yield from' construct, it's essential first to understand generators in Python. A generator is a special type of function that returns an iterator. It allows you to iterate over a sequence of values, but unlike lists or tuples, it doesn't store the whole sequence in memory. Instead, it generates each value on-the-fly, which can be very memory-efficient for large sequences.
  • Now, let's dive into the 'yield from' construct. 'yield from' is a phrase used in Python to delegate part of the generator's operations to another generator or iterable. This can simplify the code in a generator function, particularly when it's dealing with nested for loops.
  • Remember to use 'yield from' only when you want to delegate part of a generator's operations to another generator or iterable. If you're going to yield a single value, use 'yield' instead.
  • When using 'yield from' with an iterable, ensure that the iterable doesn't produce an infinite sequence. Otherwise, your generator will get stuck in an infinite loop.
  • Keep in mind that 'yield from' only works in Python 3.3 and later. If you're using an earlier version of Python, you'll have to use a for loop instead.
  • One common mistake is trying to use 'yield from' in a non-generator function. Remember, 'yield' and 'yield from' can only be used in generator functions. If you try to use them in a regular function, you'll get a SyntaxError.
  • Another common error is trying to yield from a non-iterable object. 'yield from' requires an iterable, so make sure the object you're yielding from can be iterated over. If it can't, Python will raise a TypeError.
  • To avoid these errors, always ensure that your 'yield from' statement is in a generator function and that the object you're yielding from is iterable.
  • 'yield from' is a powerful feature in Python that can make your code cleaner and more readable. It's an advanced topic, but once you understand it, it can be a great tool in your Python programming toolbox.
  • Just remember to use 'yield from' appropriately and watch out for common error-prone cases. With practice, you'll be able to use 'yield from' effectively and write more efficient Python code.

Python Tutorial

File handling, python modules, python numpy, python pandas, python matplotlib, python scipy, machine learning, python mysql, python mongodb, python reference, module reference, python how to, python examples, python yield keyword.

❮ Python Keywords

Return three values from a function:

Definition and Usage

The yield keyword is used to return a list of values from a function.

Unlike the return keyword which stops further execution of the function, the yield keyword continues to the end of the function.

When you call a function with yield keyword(s), the return value will be a list of values, one for each yield .

Related Pages

Use the return keyword to return only one value, and stop further execution.

Read more about functions in our Python Functions Tutorial .

Get Certified

COLOR PICKER

colorpicker

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: [email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail: [email protected]

Top Tutorials

Top references, top examples, get certified.

Python Enhancement Proposals

  • Python »
  • PEP Index »

PEP 572 – Assignment Expressions

The importance of real code, exceptional cases, scope of the target, relative precedence of :=, change to evaluation order, differences between assignment expressions and assignment statements, specification changes during implementation, _pydecimal.py, datetime.py, sysconfig.py, simplifying list comprehensions, capturing condition values, changing the scope rules for comprehensions, alternative spellings, special-casing conditional statements, special-casing comprehensions, lowering operator precedence, allowing commas to the right, always requiring parentheses, why not just turn existing assignment into an expression, with assignment expressions, why bother with assignment statements, why not use a sublocal scope and prevent namespace pollution, style guide recommendations, acknowledgements, a numeric example, appendix b: rough code translations for comprehensions, appendix c: no changes to scope semantics.

This is a proposal for creating a way to assign to variables within an expression using the notation NAME := expr .

As part of this change, there is also an update to dictionary comprehension evaluation order to ensure key expressions are executed before value expressions (allowing the key to be bound to a name and then re-used as part of calculating the corresponding value).

During discussion of this PEP, the operator became informally known as “the walrus operator”. The construct’s formal name is “Assignment Expressions” (as per the PEP title), but they may also be referred to as “Named Expressions” (e.g. the CPython reference implementation uses that name internally).

Naming the result of an expression is an important part of programming, allowing a descriptive name to be used in place of a longer expression, and permitting reuse. Currently, this feature is available only in statement form, making it unavailable in list comprehensions and other expression contexts.

Additionally, naming sub-parts of a large expression can assist an interactive debugger, providing useful display hooks and partial results. Without a way to capture sub-expressions inline, this would require refactoring of the original code; with assignment expressions, this merely requires the insertion of a few name := markers. Removing the need to refactor reduces the likelihood that the code be inadvertently changed as part of debugging (a common cause of Heisenbugs), and is easier to dictate to another programmer.

During the development of this PEP many people (supporters and critics both) have had a tendency to focus on toy examples on the one hand, and on overly complex examples on the other.

The danger of toy examples is twofold: they are often too abstract to make anyone go “ooh, that’s compelling”, and they are easily refuted with “I would never write it that way anyway”.

The danger of overly complex examples is that they provide a convenient strawman for critics of the proposal to shoot down (“that’s obfuscated”).

Yet there is some use for both extremely simple and extremely complex examples: they are helpful to clarify the intended semantics. Therefore, there will be some of each below.

However, in order to be compelling , examples should be rooted in real code, i.e. code that was written without any thought of this PEP, as part of a useful application, however large or small. Tim Peters has been extremely helpful by going over his own personal code repository and picking examples of code he had written that (in his view) would have been clearer if rewritten with (sparing) use of assignment expressions. His conclusion: the current proposal would have allowed a modest but clear improvement in quite a few bits of code.

Another use of real code is to observe indirectly how much value programmers place on compactness. Guido van Rossum searched through a Dropbox code base and discovered some evidence that programmers value writing fewer lines over shorter lines.

Case in point: Guido found several examples where a programmer repeated a subexpression, slowing down the program, in order to save one line of code, e.g. instead of writing:

they would write:

Another example illustrates that programmers sometimes do more work to save an extra level of indentation:

This code tries to match pattern2 even if pattern1 has a match (in which case the match on pattern2 is never used). The more efficient rewrite would have been:

Syntax and semantics

In most contexts where arbitrary Python expressions can be used, a named expression can appear. This is of the form NAME := expr where expr is any valid Python expression other than an unparenthesized tuple, and NAME is an identifier.

The value of such a named expression is the same as the incorporated expression, with the additional side-effect that the target is assigned that value:

There are a few places where assignment expressions are not allowed, in order to avoid ambiguities or user confusion:

This rule is included to simplify the choice for the user between an assignment statement and an assignment expression – there is no syntactic position where both are valid.

Again, this rule is included to avoid two visually similar ways of saying the same thing.

This rule is included to disallow excessively confusing code, and because parsing keyword arguments is complex enough already.

This rule is included to discourage side effects in a position whose exact semantics are already confusing to many users (cf. the common style recommendation against mutable default values), and also to echo the similar prohibition in calls (the previous bullet).

The reasoning here is similar to the two previous cases; this ungrouped assortment of symbols and operators composed of : and = is hard to read correctly.

This allows lambda to always bind less tightly than := ; having a name binding at the top level inside a lambda function is unlikely to be of value, as there is no way to make use of it. In cases where the name will be used more than once, the expression is likely to need parenthesizing anyway, so this prohibition will rarely affect code.

This shows that what looks like an assignment operator in an f-string is not always an assignment operator. The f-string parser uses : to indicate formatting options. To preserve backwards compatibility, assignment operator usage inside of f-strings must be parenthesized. As noted above, this usage of the assignment operator is not recommended.

An assignment expression does not introduce a new scope. In most cases the scope in which the target will be bound is self-explanatory: it is the current scope. If this scope contains a nonlocal or global declaration for the target, the assignment expression honors that. A lambda (being an explicit, if anonymous, function definition) counts as a scope for this purpose.

There is one special case: an assignment expression occurring in a list, set or dict comprehension or in a generator expression (below collectively referred to as “comprehensions”) binds the target in the containing scope, honoring a nonlocal or global declaration for the target in that scope, if one exists. For the purpose of this rule the containing scope of a nested comprehension is the scope that contains the outermost comprehension. A lambda counts as a containing scope.

The motivation for this special case is twofold. First, it allows us to conveniently capture a “witness” for an any() expression, or a counterexample for all() , for example:

Second, it allows a compact way of updating mutable state from a comprehension, for example:

However, an assignment expression target name cannot be the same as a for -target name appearing in any comprehension containing the assignment expression. The latter names are local to the comprehension in which they appear, so it would be contradictory for a contained use of the same name to refer to the scope containing the outermost comprehension instead.

For example, [i := i+1 for i in range(5)] is invalid: the for i part establishes that i is local to the comprehension, but the i := part insists that i is not local to the comprehension. The same reason makes these examples invalid too:

While it’s technically possible to assign consistent semantics to these cases, it’s difficult to determine whether those semantics actually make sense in the absence of real use cases. Accordingly, the reference implementation [1] will ensure that such cases raise SyntaxError , rather than executing with implementation defined behaviour.

This restriction applies even if the assignment expression is never executed:

For the comprehension body (the part before the first “for” keyword) and the filter expression (the part after “if” and before any nested “for”), this restriction applies solely to target names that are also used as iteration variables in the comprehension. Lambda expressions appearing in these positions introduce a new explicit function scope, and hence may use assignment expressions with no additional restrictions.

Due to design constraints in the reference implementation (the symbol table analyser cannot easily detect when names are re-used between the leftmost comprehension iterable expression and the rest of the comprehension), named expressions are disallowed entirely as part of comprehension iterable expressions (the part after each “in”, and before any subsequent “if” or “for” keyword):

A further exception applies when an assignment expression occurs in a comprehension whose containing scope is a class scope. If the rules above were to result in the target being assigned in that class’s scope, the assignment expression is expressly invalid. This case also raises SyntaxError :

(The reason for the latter exception is the implicit function scope created for comprehensions – there is currently no runtime mechanism for a function to refer to a variable in the containing class scope, and we do not want to add such a mechanism. If this issue ever gets resolved this special case may be removed from the specification of assignment expressions. Note that the problem already exists for using a variable defined in the class scope from a comprehension.)

See Appendix B for some examples of how the rules for targets in comprehensions translate to equivalent code.

The := operator groups more tightly than a comma in all syntactic positions where it is legal, but less tightly than all other operators, including or , and , not , and conditional expressions ( A if C else B ). As follows from section “Exceptional cases” above, it is never allowed at the same level as = . In case a different grouping is desired, parentheses should be used.

The := operator may be used directly in a positional function call argument; however it is invalid directly in a keyword argument.

Some examples to clarify what’s technically valid or invalid:

Most of the “valid” examples above are not recommended, since human readers of Python source code who are quickly glancing at some code may miss the distinction. But simple cases are not objectionable:

This PEP recommends always putting spaces around := , similar to PEP 8 ’s recommendation for = when used for assignment, whereas the latter disallows spaces around = used for keyword arguments.)

In order to have precisely defined semantics, the proposal requires evaluation order to be well-defined. This is technically not a new requirement, as function calls may already have side effects. Python already has a rule that subexpressions are generally evaluated from left to right. However, assignment expressions make these side effects more visible, and we propose a single change to the current evaluation order:

  • In a dict comprehension {X: Y for ...} , Y is currently evaluated before X . We propose to change this so that X is evaluated before Y . (In a dict display like {X: Y} this is already the case, and also in dict((X, Y) for ...) which should clearly be equivalent to the dict comprehension.)

Most importantly, since := is an expression, it can be used in contexts where statements are illegal, including lambda functions and comprehensions.

Conversely, assignment expressions don’t support the advanced features found in assignment statements:

  • Multiple targets are not directly supported: x = y = z = 0 # Equivalent: (z := (y := (x := 0)))
  • Single assignment targets other than a single NAME are not supported: # No equivalent a [ i ] = x self . rest = []
  • Priority around commas is different: x = 1 , 2 # Sets x to (1, 2) ( x := 1 , 2 ) # Sets x to 1
  • Iterable packing and unpacking (both regular or extended forms) are not supported: # Equivalent needs extra parentheses loc = x , y # Use (loc := (x, y)) info = name , phone , * rest # Use (info := (name, phone, *rest)) # No equivalent px , py , pz = position name , phone , email , * other_info = contact
  • Inline type annotations are not supported: # Closest equivalent is "p: Optional[int]" as a separate declaration p : Optional [ int ] = None
  • Augmented assignment is not supported: total += tax # Equivalent: (total := total + tax)

The following changes have been made based on implementation experience and additional review after the PEP was first accepted and before Python 3.8 was released:

  • for consistency with other similar exceptions, and to avoid locking in an exception name that is not necessarily going to improve clarity for end users, the originally proposed TargetScopeError subclass of SyntaxError was dropped in favour of just raising SyntaxError directly. [3]
  • due to a limitation in CPython’s symbol table analysis process, the reference implementation raises SyntaxError for all uses of named expressions inside comprehension iterable expressions, rather than only raising them when the named expression target conflicts with one of the iteration variables in the comprehension. This could be revisited given sufficiently compelling examples, but the extra complexity needed to implement the more selective restriction doesn’t seem worthwhile for purely hypothetical use cases.

Examples from the Python standard library

env_base is only used on these lines, putting its assignment on the if moves it as the “header” of the block.

  • Current: env_base = os . environ . get ( "PYTHONUSERBASE" , None ) if env_base : return env_base
  • Improved: if env_base := os . environ . get ( "PYTHONUSERBASE" , None ): return env_base

Avoid nested if and remove one indentation level.

  • Current: if self . _is_special : ans = self . _check_nans ( context = context ) if ans : return ans
  • Improved: if self . _is_special and ( ans := self . _check_nans ( context = context )): return ans

Code looks more regular and avoid multiple nested if. (See Appendix A for the origin of this example.)

  • Current: reductor = dispatch_table . get ( cls ) if reductor : rv = reductor ( x ) else : reductor = getattr ( x , "__reduce_ex__" , None ) if reductor : rv = reductor ( 4 ) else : reductor = getattr ( x , "__reduce__" , None ) if reductor : rv = reductor () else : raise Error ( "un(deep)copyable object of type %s " % cls )
  • Improved: if reductor := dispatch_table . get ( cls ): rv = reductor ( x ) elif reductor := getattr ( x , "__reduce_ex__" , None ): rv = reductor ( 4 ) elif reductor := getattr ( x , "__reduce__" , None ): rv = reductor () else : raise Error ( "un(deep)copyable object of type %s " % cls )

tz is only used for s += tz , moving its assignment inside the if helps to show its scope.

  • Current: s = _format_time ( self . _hour , self . _minute , self . _second , self . _microsecond , timespec ) tz = self . _tzstr () if tz : s += tz return s
  • Improved: s = _format_time ( self . _hour , self . _minute , self . _second , self . _microsecond , timespec ) if tz := self . _tzstr (): s += tz return s

Calling fp.readline() in the while condition and calling .match() on the if lines make the code more compact without making it harder to understand.

  • Current: while True : line = fp . readline () if not line : break m = define_rx . match ( line ) if m : n , v = m . group ( 1 , 2 ) try : v = int ( v ) except ValueError : pass vars [ n ] = v else : m = undef_rx . match ( line ) if m : vars [ m . group ( 1 )] = 0
  • Improved: while line := fp . readline (): if m := define_rx . match ( line ): n , v = m . group ( 1 , 2 ) try : v = int ( v ) except ValueError : pass vars [ n ] = v elif m := undef_rx . match ( line ): vars [ m . group ( 1 )] = 0

A list comprehension can map and filter efficiently by capturing the condition:

Similarly, a subexpression can be reused within the main expression, by giving it a name on first use:

Note that in both cases the variable y is bound in the containing scope (i.e. at the same level as results or stuff ).

Assignment expressions can be used to good effect in the header of an if or while statement:

Particularly with the while loop, this can remove the need to have an infinite loop, an assignment, and a condition. It also creates a smooth parallel between a loop which simply uses a function call as its condition, and one which uses that as its condition but also uses the actual value.

An example from the low-level UNIX world:

Rejected alternative proposals

Proposals broadly similar to this one have come up frequently on python-ideas. Below are a number of alternative syntaxes, some of them specific to comprehensions, which have been rejected in favour of the one given above.

A previous version of this PEP proposed subtle changes to the scope rules for comprehensions, to make them more usable in class scope and to unify the scope of the “outermost iterable” and the rest of the comprehension. However, this part of the proposal would have caused backwards incompatibilities, and has been withdrawn so the PEP can focus on assignment expressions.

Broadly the same semantics as the current proposal, but spelled differently.

Since EXPR as NAME already has meaning in import , except and with statements (with different semantics), this would create unnecessary confusion or require special-casing (e.g. to forbid assignment within the headers of these statements).

(Note that with EXPR as VAR does not simply assign the value of EXPR to VAR – it calls EXPR.__enter__() and assigns the result of that to VAR .)

Additional reasons to prefer := over this spelling include:

  • In if f(x) as y the assignment target doesn’t jump out at you – it just reads like if f x blah blah and it is too similar visually to if f(x) and y .
  • import foo as bar
  • except Exc as var
  • with ctxmgr() as var

To the contrary, the assignment expression does not belong to the if or while that starts the line, and we intentionally allow assignment expressions in other contexts as well.

  • NAME = EXPR
  • if NAME := EXPR

reinforces the visual recognition of assignment expressions.

This syntax is inspired by languages such as R and Haskell, and some programmable calculators. (Note that a left-facing arrow y <- f(x) is not possible in Python, as it would be interpreted as less-than and unary minus.) This syntax has a slight advantage over ‘as’ in that it does not conflict with with , except and import , but otherwise is equivalent. But it is entirely unrelated to Python’s other use of -> (function return type annotations), and compared to := (which dates back to Algol-58) it has a much weaker tradition.

This has the advantage that leaked usage can be readily detected, removing some forms of syntactic ambiguity. However, this would be the only place in Python where a variable’s scope is encoded into its name, making refactoring harder.

Execution order is inverted (the indented body is performed first, followed by the “header”). This requires a new keyword, unless an existing keyword is repurposed (most likely with: ). See PEP 3150 for prior discussion on this subject (with the proposed keyword being given: ).

This syntax has fewer conflicts than as does (conflicting only with the raise Exc from Exc notation), but is otherwise comparable to it. Instead of paralleling with expr as target: (which can be useful but can also be confusing), this has no parallels, but is evocative.

One of the most popular use-cases is if and while statements. Instead of a more general solution, this proposal enhances the syntax of these two statements to add a means of capturing the compared value:

This works beautifully if and ONLY if the desired condition is based on the truthiness of the captured value. It is thus effective for specific use-cases (regex matches, socket reads that return '' when done), and completely useless in more complicated cases (e.g. where the condition is f(x) < 0 and you want to capture the value of f(x) ). It also has no benefit to list comprehensions.

Advantages: No syntactic ambiguities. Disadvantages: Answers only a fraction of possible use-cases, even in if / while statements.

Another common use-case is comprehensions (list/set/dict, and genexps). As above, proposals have been made for comprehension-specific solutions.

This brings the subexpression to a location in between the ‘for’ loop and the expression. It introduces an additional language keyword, which creates conflicts. Of the three, where reads the most cleanly, but also has the greatest potential for conflict (e.g. SQLAlchemy and numpy have where methods, as does tkinter.dnd.Icon in the standard library).

As above, but reusing the with keyword. Doesn’t read too badly, and needs no additional language keyword. Is restricted to comprehensions, though, and cannot as easily be transformed into “longhand” for-loop syntax. Has the C problem that an equals sign in an expression can now create a name binding, rather than performing a comparison. Would raise the question of why “with NAME = EXPR:” cannot be used as a statement on its own.

As per option 2, but using as rather than an equals sign. Aligns syntactically with other uses of as for name binding, but a simple transformation to for-loop longhand would create drastically different semantics; the meaning of with inside a comprehension would be completely different from the meaning as a stand-alone statement, while retaining identical syntax.

Regardless of the spelling chosen, this introduces a stark difference between comprehensions and the equivalent unrolled long-hand form of the loop. It is no longer possible to unwrap the loop into statement form without reworking any name bindings. The only keyword that can be repurposed to this task is with , thus giving it sneakily different semantics in a comprehension than in a statement; alternatively, a new keyword is needed, with all the costs therein.

There are two logical precedences for the := operator. Either it should bind as loosely as possible, as does statement-assignment; or it should bind more tightly than comparison operators. Placing its precedence between the comparison and arithmetic operators (to be precise: just lower than bitwise OR) allows most uses inside while and if conditions to be spelled without parentheses, as it is most likely that you wish to capture the value of something, then perform a comparison on it:

Once find() returns -1, the loop terminates. If := binds as loosely as = does, this would capture the result of the comparison (generally either True or False ), which is less useful.

While this behaviour would be convenient in many situations, it is also harder to explain than “the := operator behaves just like the assignment statement”, and as such, the precedence for := has been made as close as possible to that of = (with the exception that it binds tighter than comma).

Some critics have claimed that the assignment expressions should allow unparenthesized tuples on the right, so that these two would be equivalent:

(With the current version of the proposal, the latter would be equivalent to ((point := x), y) .)

However, adopting this stance would logically lead to the conclusion that when used in a function call, assignment expressions also bind less tight than comma, so we’d have the following confusing equivalence:

The less confusing option is to make := bind more tightly than comma.

It’s been proposed to just always require parentheses around an assignment expression. This would resolve many ambiguities, and indeed parentheses will frequently be needed to extract the desired subexpression. But in the following cases the extra parentheses feel redundant:

Frequently Raised Objections

C and its derivatives define the = operator as an expression, rather than a statement as is Python’s way. This allows assignments in more contexts, including contexts where comparisons are more common. The syntactic similarity between if (x == y) and if (x = y) belies their drastically different semantics. Thus this proposal uses := to clarify the distinction.

The two forms have different flexibilities. The := operator can be used inside a larger expression; the = statement can be augmented to += and its friends, can be chained, and can assign to attributes and subscripts.

Previous revisions of this proposal involved sublocal scope (restricted to a single statement), preventing name leakage and namespace pollution. While a definite advantage in a number of situations, this increases complexity in many others, and the costs are not justified by the benefits. In the interests of language simplicity, the name bindings created here are exactly equivalent to any other name bindings, including that usage at class or module scope will create externally-visible names. This is no different from for loops or other constructs, and can be solved the same way: del the name once it is no longer needed, or prefix it with an underscore.

(The author wishes to thank Guido van Rossum and Christoph Groth for their suggestions to move the proposal in this direction. [2] )

As expression assignments can sometimes be used equivalently to statement assignments, the question of which should be preferred will arise. For the benefit of style guides such as PEP 8 , two recommendations are suggested.

  • If either assignment statements or assignment expressions can be used, prefer statements; they are a clear declaration of intent.
  • If using assignment expressions would lead to ambiguity about execution order, restructure it to use statements instead.

The authors wish to thank Alyssa Coghlan and Steven D’Aprano for their considerable contributions to this proposal, and members of the core-mentorship mailing list for assistance with implementation.

Appendix A: Tim Peters’s findings

Here’s a brief essay Tim Peters wrote on the topic.

I dislike “busy” lines of code, and also dislike putting conceptually unrelated logic on a single line. So, for example, instead of:

instead. So I suspected I’d find few places I’d want to use assignment expressions. I didn’t even consider them for lines already stretching halfway across the screen. In other cases, “unrelated” ruled:

is a vast improvement over the briefer:

The original two statements are doing entirely different conceptual things, and slamming them together is conceptually insane.

In other cases, combining related logic made it harder to understand, such as rewriting:

as the briefer:

The while test there is too subtle, crucially relying on strict left-to-right evaluation in a non-short-circuiting or method-chaining context. My brain isn’t wired that way.

But cases like that were rare. Name binding is very frequent, and “sparse is better than dense” does not mean “almost empty is better than sparse”. For example, I have many functions that return None or 0 to communicate “I have nothing useful to return in this case, but since that’s expected often I’m not going to annoy you with an exception”. This is essentially the same as regular expression search functions returning None when there is no match. So there was lots of code of the form:

I find that clearer, and certainly a bit less typing and pattern-matching reading, as:

It’s also nice to trade away a small amount of horizontal whitespace to get another _line_ of surrounding code on screen. I didn’t give much weight to this at first, but it was so very frequent it added up, and I soon enough became annoyed that I couldn’t actually run the briefer code. That surprised me!

There are other cases where assignment expressions really shine. Rather than pick another from my code, Kirill Balunov gave a lovely example from the standard library’s copy() function in copy.py :

The ever-increasing indentation is semantically misleading: the logic is conceptually flat, “the first test that succeeds wins”:

Using easy assignment expressions allows the visual structure of the code to emphasize the conceptual flatness of the logic; ever-increasing indentation obscured it.

A smaller example from my code delighted me, both allowing to put inherently related logic in a single line, and allowing to remove an annoying “artificial” indentation level:

That if is about as long as I want my lines to get, but remains easy to follow.

So, in all, in most lines binding a name, I wouldn’t use assignment expressions, but because that construct is so very frequent, that leaves many places I would. In most of the latter, I found a small win that adds up due to how often it occurs, and in the rest I found a moderate to major win. I’d certainly use it more often than ternary if , but significantly less often than augmented assignment.

I have another example that quite impressed me at the time.

Where all variables are positive integers, and a is at least as large as the n’th root of x, this algorithm returns the floor of the n’th root of x (and roughly doubling the number of accurate bits per iteration):

It’s not obvious why that works, but is no more obvious in the “loop and a half” form. It’s hard to prove correctness without building on the right insight (the “arithmetic mean - geometric mean inequality”), and knowing some non-trivial things about how nested floor functions behave. That is, the challenges are in the math, not really in the coding.

If you do know all that, then the assignment-expression form is easily read as “while the current guess is too large, get a smaller guess”, where the “too large?” test and the new guess share an expensive sub-expression.

To my eyes, the original form is harder to understand:

This appendix attempts to clarify (though not specify) the rules when a target occurs in a comprehension or in a generator expression. For a number of illustrative examples we show the original code, containing a comprehension, and the translation, where the comprehension has been replaced by an equivalent generator function plus some scaffolding.

Since [x for ...] is equivalent to list(x for ...) these examples all use list comprehensions without loss of generality. And since these examples are meant to clarify edge cases of the rules, they aren’t trying to look like real code.

Note: comprehensions are already implemented via synthesizing nested generator functions like those in this appendix. The new part is adding appropriate declarations to establish the intended scope of assignment expression targets (the same scope they resolve to as if the assignment were performed in the block containing the outermost comprehension). For type inference purposes, these illustrative expansions do not imply that assignment expression targets are always Optional (but they do indicate the target binding scope).

Let’s start with a reminder of what code is generated for a generator expression without assignment expression.

  • Original code (EXPR usually references VAR): def f (): a = [ EXPR for VAR in ITERABLE ]
  • Translation (let’s not worry about name conflicts): def f (): def genexpr ( iterator ): for VAR in iterator : yield EXPR a = list ( genexpr ( iter ( ITERABLE )))

Let’s add a simple assignment expression.

  • Original code: def f (): a = [ TARGET := EXPR for VAR in ITERABLE ]
  • Translation: def f (): if False : TARGET = None # Dead code to ensure TARGET is a local variable def genexpr ( iterator ): nonlocal TARGET for VAR in iterator : TARGET = EXPR yield TARGET a = list ( genexpr ( iter ( ITERABLE )))

Let’s add a global TARGET declaration in f() .

  • Original code: def f (): global TARGET a = [ TARGET := EXPR for VAR in ITERABLE ]
  • Translation: def f (): global TARGET def genexpr ( iterator ): global TARGET for VAR in iterator : TARGET = EXPR yield TARGET a = list ( genexpr ( iter ( ITERABLE )))

Or instead let’s add a nonlocal TARGET declaration in f() .

  • Original code: def g (): TARGET = ... def f (): nonlocal TARGET a = [ TARGET := EXPR for VAR in ITERABLE ]
  • Translation: def g (): TARGET = ... def f (): nonlocal TARGET def genexpr ( iterator ): nonlocal TARGET for VAR in iterator : TARGET = EXPR yield TARGET a = list ( genexpr ( iter ( ITERABLE )))

Finally, let’s nest two comprehensions.

  • Original code: def f (): a = [[ TARGET := i for i in range ( 3 )] for j in range ( 2 )] # I.e., a = [[0, 1, 2], [0, 1, 2]] print ( TARGET ) # prints 2
  • Translation: def f (): if False : TARGET = None def outer_genexpr ( outer_iterator ): nonlocal TARGET def inner_generator ( inner_iterator ): nonlocal TARGET for i in inner_iterator : TARGET = i yield i for j in outer_iterator : yield list ( inner_generator ( range ( 3 ))) a = list ( outer_genexpr ( range ( 2 ))) print ( TARGET )

Because it has been a point of confusion, note that nothing about Python’s scoping semantics is changed. Function-local scopes continue to be resolved at compile time, and to have indefinite temporal extent at run time (“full closures”). Example:

This document has been placed in the public domain.

Source: https://github.com/python/peps/blob/main/peps/pep-0572.rst

Last modified: 2023-10-11 12:05:51 GMT

  • Python Basics
  • Interview Questions
  • Python Quiz
  • Popular Packages
  • Python Projects
  • Practice Python
  • AI With Python
  • Learn Python3
  • Python Automation
  • Python Web Dev
  • DSA with Python
  • Python OOPs
  • Dictionaries

Python Operators

Precedence and associativity of operators in python.

  • Python Arithmetic Operators
  • Difference between / vs. // operator in Python
  • Python - Star or Asterisk operator ( * )
  • What does the Double Star operator mean in Python?
  • Division Operators in Python
  • Modulo operator (%) in Python
  • Python Logical Operators
  • Python OR Operator
  • Difference between 'and' and '&' in Python
  • not Operator in Python | Boolean Logic

Ternary Operator in Python

  • Python Bitwise Operators

Python Assignment Operators

Assignment operators in python.

  • Walrus Operator in Python 3.8
  • Increment += and Decrement -= Assignment Operators in Python
  • Merging and Updating Dictionary Operators in Python 3.9
  • New '=' Operator in Python3.8 f-string

Python Relational Operators

  • Comparison Operators in Python
  • Python NOT EQUAL operator
  • Difference between == and is operator in Python
  • Chaining comparison operators in Python
  • Python Membership and Identity Operators
  • Difference between != and is not operator in Python

In Python programming, Operators in general are used to perform operations on values and variables. These are standard symbols used for logical and arithmetic operations. In this article, we will look into different types of Python operators. 

  • OPERATORS: These are the special symbols. Eg- + , * , /, etc.
  • OPERAND: It is the value on which the operator is applied.

Types of Operators in Python

  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Identity Operators and Membership Operators

Python Operators

Arithmetic Operators in Python

Python Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication , and division .

In Python 3.x the result of division is a floating-point while in Python 2.x division of 2 integers was an integer. To obtain an integer result in Python 3.x floored (// integer) is used.

Example of Arithmetic Operators in Python

Division operators.

In Python programming language Division Operators allow you to divide two numbers and return a quotient, i.e., the first number or number at the left is divided by the second number or number at the right and returns the quotient. 

There are two types of division operators: 

Float division

  • Floor division

The quotient returned by this operator is always a float number, no matter if two numbers are integers. For example:

Example: The code performs division operations and prints the results. It demonstrates that both integer and floating-point divisions return accurate results. For example, ’10/2′ results in ‘5.0’ , and ‘-10/2’ results in ‘-5.0’ .

Integer division( Floor division)

The quotient returned by this operator is dependent on the argument being passed. If any of the numbers is float, it returns output in float. It is also known as Floor division because, if any number is negative, then the output will be floored. For example:

Example: The code demonstrates integer (floor) division operations using the // in Python operators . It provides results as follows: ’10//3′ equals ‘3’ , ‘-5//2’ equals ‘-3’ , ‘ 5.0//2′ equals ‘2.0’ , and ‘-5.0//2’ equals ‘-3.0’ . Integer division returns the largest integer less than or equal to the division result.

Precedence of Arithmetic Operators in Python

The precedence of Arithmetic Operators in Python is as follows:

  • P – Parentheses
  • E – Exponentiation
  • M – Multiplication (Multiplication and division have the same precedence)
  • D – Division
  • A – Addition (Addition and subtraction have the same precedence)
  • S – Subtraction

The modulus of Python operators helps us extract the last digit/s of a number. For example:

  • x % 10 -> yields the last digit
  • x % 100 -> yield last two digits

Arithmetic Operators With Addition, Subtraction, Multiplication, Modulo and Power

Here is an example showing how different Arithmetic Operators in Python work:

Example: The code performs basic arithmetic operations with the values of ‘a’ and ‘b’ . It adds (‘+’) , subtracts (‘-‘) , multiplies (‘*’) , computes the remainder (‘%’) , and raises a to the power of ‘b (**)’ . The results of these operations are printed.

Note: Refer to Differences between / and // for some interesting facts about these two Python operators.

Comparison of Python Operators

In Python Comparison of Relational operators compares the values. It either returns True or False according to the condition.

= is an assignment operator and == comparison operator.

Precedence of Comparison Operators in Python

In Python, the comparison operators have lower precedence than the arithmetic operators. All the operators within comparison operators have the same precedence order.

Example of Comparison Operators in Python

Let’s see an example of Comparison Operators in Python.

Example: The code compares the values of ‘a’ and ‘b’ using various comparison Python operators and prints the results. It checks if ‘a’ is greater than, less than, equal to, not equal to, greater than, or equal to, and less than or equal to ‘b’ .

Logical Operators in Python

Python Logical operators perform Logical AND , Logical OR , and Logical NOT operations. It is used to combine conditional statements.

Precedence of Logical Operators in Python

The precedence of Logical Operators in Python is as follows:

  • Logical not
  • logical and

Example of Logical Operators in Python

The following code shows how to implement Logical Operators in Python:

Example: The code performs logical operations with Boolean values. It checks if both ‘a’ and ‘b’ are true ( ‘and’ ), if at least one of them is true ( ‘or’ ), and negates the value of ‘a’ using ‘not’ . The results are printed accordingly.

Bitwise Operators in Python

Python Bitwise operators act on bits and perform bit-by-bit operations. These are used to operate on binary numbers.

Precedence of Bitwise Operators in Python

The precedence of Bitwise Operators in Python is as follows:

  • Bitwise NOT
  • Bitwise Shift
  • Bitwise AND
  • Bitwise XOR

Here is an example showing how Bitwise Operators in Python work:

Example: The code demonstrates various bitwise operations with the values of ‘a’ and ‘b’ . It performs bitwise AND (&) , OR (|) , NOT (~) , XOR (^) , right shift (>>) , and left shift (<<) operations and prints the results. These operations manipulate the binary representations of the numbers.

Python Assignment operators are used to assign values to the variables.

Let’s see an example of Assignment Operators in Python.

Example: The code starts with ‘a’ and ‘b’ both having the value 10. It then performs a series of operations: addition, subtraction, multiplication, and a left shift operation on ‘b’ . The results of each operation are printed, showing the impact of these operations on the value of ‘b’ .

Identity Operators in Python

In Python, is and is not are the identity operators both are used to check if two values are located on the same part of the memory. Two variables that are equal do not imply that they are identical. 

Example Identity Operators in Python

Let’s see an example of Identity Operators in Python.

Example: The code uses identity operators to compare variables in Python. It checks if ‘a’ is not the same object as ‘b’ (which is true because they have different values) and if ‘a’ is the same object as ‘c’ (which is true because ‘c’ was assigned the value of ‘a’ ).

Membership Operators in Python

In Python, in and not in are the membership operators that are used to test whether a value or variable is in a sequence.

Examples of Membership Operators in Python

The following code shows how to implement Membership Operators in Python:

Example: The code checks for the presence of values ‘x’ and ‘y’ in the list. It prints whether or not each value is present in the list. ‘x’ is not in the list, and ‘y’ is present, as indicated by the printed messages. The code uses the ‘in’ and ‘not in’ Python operators to perform these checks.

in Python, Ternary operators also known as conditional expressions are operators that evaluate something based on a condition being true or false. It was added to Python in version 2.5. 

It simply allows testing a condition in a single line replacing the multiline if-else making the code compact.

Syntax :   [on_true] if [expression] else [on_false] 

Examples of Ternary Operator in Python

The code assigns values to variables ‘a’ and ‘b’ (10 and 20, respectively). It then uses a conditional assignment to determine the smaller of the two values and assigns it to the variable ‘min’ . Finally, it prints the value of ‘min’ , which is 10 in this case.

In Python, Operator precedence and associativity determine the priorities of the operator.

Operator Precedence in Python

This is used in an expression with more than one operator with different precedence to determine which operation to perform first.

Let’s see an example of how Operator Precedence in Python works:

Example: The code first calculates and prints the value of the expression 10 + 20 * 30 , which is 610. Then, it checks a condition based on the values of the ‘name’ and ‘age’ variables. Since the name is “ Alex” and the condition is satisfied using the or operator, it prints “Hello! Welcome.”

Operator Associativity in Python

If an expression contains two or more operators with the same precedence then Operator Associativity is used to determine. It can either be Left to Right or from Right to Left.

The following code shows how Operator Associativity in Python works:

Example: The code showcases various mathematical operations. It calculates and prints the results of division and multiplication, addition and subtraction, subtraction within parentheses, and exponentiation. The code illustrates different mathematical calculations and their outcomes.

To try your knowledge of Python Operators, you can take out the quiz on Operators in Python . 

Python Operator Exercise Questions

Below are two Exercise Questions on Python Operators. We have covered arithmetic operators and comparison operators in these exercise questions. For more exercises on Python Operators visit the page mentioned below.

Q1. Code to implement basic arithmetic operations on integers

Q2. Code to implement Comparison operations on integers

Explore more Exercises: Practice Exercise on Operators in Python

Please Login to comment...

Similar reads.

  • python-basics
  • Python-Operators

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

Do assignments ever require locking?

I recently came across some code where the author applies locks around “simple” assignments, in a multithreaded program:

I.e. the lock merely protects reading and writing the member variable.

Is there any situation, maybe in pypy or some other non-CPython implementation of Python, where the above code might be required to avoid a race condition?

The author of the code in question doesn’t want to remove the lock because they think it might be necessary on Jython or pypy or a future no-GIL CPython or [unknown].

My take is that assignments are atomic by definition, no matter the underlying Python implementation, and the lock is unnecessary overhead and impedes the code’s readability.

Who is correct?

I’d agree with you for reading a primitive.

In the code example, without the lock multiple threads could write to that method variable. And presumably it’s not about reading that method variable (like a primitive). It’s called call_back so the intention is to call it. This could do anything to other state on obj, and require other expensive or shared resources.

So the author could well have had good reason. If nothing’s broken, I would not mess around with it just to improve code quality.

My understanding is that assignments are not by definition atomic - that they are only atomic for simple assignments (using immutable objects like small integers or using built-in data types). Assignments for mutable types/complex objects correspond to multiple byte code instructions. My understanding that a thread switch can occur between any two byte code instructions.

I tried to find a good reference in the official Python docs. Best I could find was this: What kind of global mutation are thread-safe? which is rather fuzzy.

I’m not sure, but I’m tempted to think the sample code does require locking (if thread-safety is a goal). The lock on the read could in some cases also be required, I think. Perhaps not in this example (I dont know), what if you have a longer block of code where the function would be called multiple times and you want to ensure consistency? Also, ‘some_callback’ itself would make me nervous, since it can be anything – for instance a C-level call that itself releases the GIL; it could by itself be thread-safe, but once it releases the GIL anyone is free to modify the parent object.

Unfortunately, without being completely sure what the object is, we can’t actually say. If some_callback is actually a property, it could do anything (as it’s effectively a method call in disguise). Personally I would consider this to be bad style (if it’s a property and it absolutely needs atomicity, it should do the locking inside the property function instead), but it’s legal.

But my gut feeling is that this is cargo cult programming. Having run into threading problems and solved them with locks, the author threw them into more places. It’s really hard to be completely sure, though.

Let’s assume that obj.some_callback is not a property; if it was it could lock itself internally. Likewise if there was an issue with the callback getting run from two threads then it could use an internal lock if it needed to.

My question thus boils down to this: Can “thread A updates an attribute while thread B reads it” result in any other outcome than “thread B reads either the old or the new value of the attribute” plus “there are no related consistency problems” (like broken reference counters)? Or, more to the point, does the specification of Python-the-language, as far as such specification exists, guarantee that anywhere?

No, there is no such formal python specification. However, by default CPython’s behavior is then what should be expected from all implementations, which in this case clearly says “no, pure python code should never be able to result in broken (e.g. wrong ref count) objects”

Starting with the last question: We’re currently (as of 2024) actually seeing quite a bit of specification needing to be written. Aspects that had previously not required any sort of documentation are now going to become important. Free threading is revealing that there are definitely some aspects of Python-the-language that have never actually been promised, and CPython implementation details are what we all go on. So, take all of what I say, and all of what everyone else in this thread says, with the proviso that more things may be specified in the future. I would generally assume that any new specs will be compatible with existing behaviour, though, so anything that IS specified should remain valid.

So. Let’s figure out exactly what happens when you mutate an object’s aftribute. As mentioned, properties and such can get in the way, but I’m going to assume the most simple and straight-forward behaviour there is: The object’s dictionary gets updated.

So your question comes down to two things:

  • Can thread A and thread B see two distinct dictionaries?
  • If one thread is changing a dictionary, will another thread see problems?

I’ll start by excluding a number of behaviours which, if they were to occur, would definitely be called interpreter bugs:

  • A “half-and-half” pointer error. On some CPU architectures, a pointer (such as a Python object reference) can’t be written in one go, so you have to write one half, then the other half. So you could read the first half of the new value paired with the second half of the old value, which would give you a nonsense pointer. This would be bad, very very bad; but it would also be one of the most obvious problems to look for. I think we can rule this one out on the basis that the exact same problem will show up in practically everything, and so it will NEED to be solved. (Which is quite easy on those CPU architectures that can write an entire pointer atomically.)
  • A broken reference count. This one’s specific to reference counting, obviously, and one way to prevent it is to go for a different style of garbage collection (Python doesn’t mandate refcounting). But, again, if refcounting is a thing, broken refcounts are a critical failure, so this one will be dealt with.
  • A failure within the dictionary itself. For example, thread A might be trying to add a new attribute, which causes the dictionary to enlarge itself. This would be a more insidious bug as it’s not going to trigger nearly as frequently as the other types, but since dictionaries are so important in Python, I would trust that people smarter than I will have already looked into this and made sure it won’t be a problem. (Which, again, might be quite simple if there’s a way to leave the old dictionary contents visible to other threads while the new hash table is being constructed.)

In general, anything that would outright segfault the interpreter should be considered a bug. [1] Bugs definitely do happen, but the more common the situation, the more likely that it’ll be discovered early. Core data types like dictionaries, lists, strings, etc should be safe from these kinds of critical failures.

That’s not to say there can’t be other sorts of errors, and a simple x += 1 could well produce strange results, but at very very least, it won’t result in a memory leak or a half-and-half pointer or anything like that.

Unless you’re using ctypes - in which case, have fun, there’s endless crashes waiting for you! ↩︎

The GIL ensures that python will be able to execute its byte code ops atomically. The lock is pointless, unless its a property. As Chris suggest may be cargo-culted.

If there was a read-modify-write pattern then a lock is mandatory for correctness.

If 2 threads read the some_callback then there is nothing to stop the callback being invoken by both threads. Which may, or may not, be intended. More of the design would need be explained.

(This is a decent approximation, but there are exceptions. The GIL isn’t held for “one bytecode operation”; but you can certainly assume that the GIL could be released between any two operations as listed in dis.dis() .)

Umm, well, there are Python versions without a GIL today, not to mention the current work to remove it entirely, long-term.

There will need to be an equivalent guarantee spelled out that we can reason about for multi-threaded code for the free-threading python.

Which, I assume, will be documented at some point.

Related Topics

  • Python »
  • 3.14.0a0 Documentation »
  • Python 教學 »
  • 9. Class(類別)
  • Theme Auto Light Dark |

9. Class(類別) ¶

Class 提供了一種結合資料與功能的手段。建立一個 class 將會新增一個物件的 型別 (type) ,並且允許建立該型別的新 實例 (instance) 。每一個 class 實例可以擁有一些維持該實例狀態的屬性 (attribute)。Class 實例也可以有一些(由其 class 所定義的)method(方法),用於修改該實例的狀態。

與其他程式語言相比,Python 的 class 機制為 class 增加了最少的新語法跟語意。他混合了 C++ 和 Modula-3 的 class 機制。Python 的 class 提供了所有物件導向程式設計 (Object Oriented Programming) 的標準特色:class 繼承機制允許多個 base class(基底類別),一個 derived class(衍生類別)可以覆寫 (override) 其 base class 的任何 method,且一個 method 可以用相同的名稱呼叫其 base class 的 method。物件可以包含任意數量及任意種類的資料。如同模組一樣,class 也具有 Python 的動態特性:他們在執行期 (runtime) 被建立,且可以在建立之後被修改。

在 C++ 的術語中,class 成員(包含資料成員)通常都是 公開 的(除了以下內容: 私有變數 ),而所有的成員函式都是 虛擬 的。如同在 Modula-3 中一樣,Python 並沒有提供簡寫可以從物件的 method 裡參照其成員:method 函式與一個外顯的 (explicit)、第一個代表物件的引數被宣告,而此引數是在呼叫時隱性地 (implicitly) 被提供。如同在 Smalltak 中,class 都是物件,這為 import 及重新命名提供了語意。不像 C++ 和 Modula-3,Pyhon 內建的型別可以被使用者以 base class 用於其他擴充 (extension)。另外,如同在 C++ 中,大多數有著特別語法的內建運算子(算術運算子、下標等)都可以為了 class 實例而被重新定義。

(由於缺乏普遍能接受的術語來討論 class,我偶爾會使用 Smalltalk 和 C++ 的術語。我會使用 Modula-3 的術語,因為它比 C++ 更接近 Python 的物件導向語意,但我預期比較少的讀者會聽過它。)

9.1. 關於名稱與物件的一段話 ¶

物件有個體性 (individuality),且多個名稱(在多個作用域 (scope) )可以被連結到相同的物件。這在其他語言中被稱為別名 (aliasing)。初次接觸 Python 時通常不會注意這件事,而在處理不可變的基本型別(數值、字串、tuple)時,它也可以安全地被忽略。然而,別名在含有可變物件(如 list(串列)、dictionary(字典)、和大多數其他的型別)的 Python 程式碼語意中,可能會有意外的效果。這通常有利於程式,因為別名在某些方面表現得像指標 (pointer)。舉例來說,在實作時傳遞一個物件是便宜的,因為只有指標被傳遞;假如函式修改了一個作為引數傳遞的物件,呼叫函式者 (caller) 能夠見到這些改變——這消除了在 Pascal 中兩個相異引數傳遞機制的需求。

9.2. Python 作用域 (Scope) 及命名空間 (Namespace) ¶

在介紹 class 之前,我必須先告訴你一些關於 Python 作用域的規則。Class definition(類別定義)以命名空間展現了一些俐落的技巧,而你需要了解作用域和命名空間的運作才能完整理解正在發生的事情。順帶一提,關於這個主題的知識對任何進階的 Python 程式設計師都是很有用的。

讓我們從一些定義開始。

命名空間 是從名稱到物件的映射。大部分的命名空間現在都是以 Python 的 dictionary 被實作,但通常不會以任何方式被察覺(除了性能),且它可能會在未來改變。命名空間的例子有:內建名稱的集合(包含如 abs() 的函式,和內建的例外名稱);模組中的全域 (global) 名稱;和在函式調用中的區域 (local) 名稱。某種意義上,物件中的屬性集合也會形成一個命名空間。關於命名空間的重要一點是,不同命名空間中的名稱之間絕對沒有關係;舉例來說,兩個不一樣的模組都可以定義一個 maximize 函式而不會混淆——模組的使用者必須為它加上前綴 (prefix) 模組名稱。

順帶一提,我使用 屬性 (attribute) 這個字,統稱句號 (dot) 後面的任何名稱——例如,運算式中的 z.real , real 是物件 z 的一個屬性。嚴格來說,模組中名稱的參照都是屬性參照:在運算式 modname.funcname 中, modname 是模組物件而 funcname 是它的屬性。在這種情況下,模組的屬性和模組中定義的全域名稱碰巧有一個直接的對映:他們共享了相同的命名空間! [ 1 ]

屬性可以是唯讀的或可寫的。在後者的情況下,對屬性的賦值是可能的。模組屬性是可寫的:你可以寫 modname.the_answer = 42 。可寫屬性也可以用 del 陳述式刪除。例如, del modname.the_answer 將從名為 modname 的物件中刪除屬性 the_answer 。

命名空間在不同的時刻被建立,並且有不同的壽命。當 Python 直譯器啟動時,含有內建名稱的命名空間會被建立,並且永遠不會被刪除。當模組定義被讀入時,模組的全域命名空間會被建立;一般情況下,模組的命名空間也會持續到直譯器結束。被直譯器的頂層調用 (top-level invocation) 執行的陳述式,不論是從腳本檔案讀取的或是互動模式中的,會被視為一個稱為 __main__ 的模組的一部分,因此它們具有自己的全域命名空間。(內建名稱實際上也存在一個模組中,它被稱為 builtins 。)

函式的區域命名空間是在呼叫函式時建立的,而當函式返回,或引發了未在函式中處理的例外時,此命名空間將會被刪除。(實際上,忘記是描述實際發生的事情的更好方法。) 當然,每個遞迴調用 (recursive invocation) 都有自己的區域命名空間。

作用域 是 Python 程式中的一個文本區域 (textual region),在此區域,命名空間是可直接存取的。這裡的「可直接存取的」意思是,對一個名稱的非限定參照 (unqualified reference) 可以在命名空間內嘗試尋找該名稱。

儘管作用域是靜態地被決定,但它們是動態地被使用的。在執行期間內的任何時間點,都會有 3 或 4 個巢狀的作用域,其命名空間是可以被直接存取的:

最內層作用域,會最先被搜尋,而它包含了區域名稱

任何外圍函式 (enclosing function) 的作用域,會從最近的外圍作用域開始搜尋,它包含了非區域 (non-local) 和非全域 (non-global) 的名稱

倒數第二個作用域,包含當前模組的全域名稱

最外面的作用域(最後搜尋),是包含內建名稱的命名空間

如果一個名稱被宣告為全域,則所有的參照和賦值將直接轉到包含模組全域名稱的倒數第二個作用域。要重新連結最內層作用域以外找到的變數,可以使用 nonlocal 陳述式;如果那些變數沒有被宣告為 nonlocal,則它們會是唯讀的(嘗試寫入這樣的變數只會在最內層的作用域內建立一個 新的 區域變數,同名的外部變數則維持不變)。

通常,區域作用域會參照(文本的)當前函式的區域名稱。在函式外部,區域作用域與全域作用域參照相同的命名空間:模組的命名空間。然而,Class definition 會在區域作用域中放置另一個命名空間。

務必要了解,作用域是按文本被決定的:在模組中定義的函式,其全域作用域便是該模組的命名空間,無論函式是從何處或以什麼別名被呼叫。另一方面,對名稱的實際搜尋是在執行時期 (run time) 動態完成的——但是,語言定義的發展,正朝向在「編譯」時期 (compile time) 的靜態名稱解析 (static name resolution),所以不要太依賴動態名稱解析 (dynamic name resolution)! (事實上,局部變數已經是靜態地被決定。)

一個 Python 的特殊癖好是——假如沒有 global 或 nonlocal 陳述式的效果——名稱的賦值 (assignment) 都會指向最內層作用域。賦值不會複製資料——它們只會把名稱連結至物件。刪除也是一樣:陳述式 del x 會從區域作用域參照的命名空間移除 x 的連結。事實上,引入新名稱的所有運算都使用區域作用域:特別是 import 陳述式和函式定義,會連結區域作用域內的模組或函式名稱。

global 陳述式可以用來表示特定變數存活在全域作用域,應該被重新綁定到那裡; nonlocal 陳述式表示特定變數存活在外圍作用域內,應該被重新綁定到那裡。

9.2.1. 作用域和命名空間的範例 ¶

這是一個範例,演示如何參照不同的作用域和命名空間,以及 global 和 nonlocal 如何影響變數的綁定:

請注意, 區域 賦值(預設情況)不會改變 scope_test 對 spam 的連結。 nonlocal 賦值改變了 scope_test 對 spam 的連結,而 global 賦值改變了模組層次的連結。

你還可以發現,在 global 賦值之前,沒有對 spam 的連結。

9.3. 初見 class ¶

Class 採用一些新的語法,三個新的物件型別,以及一些新的語意。

9.3.1. Class definition(類別定義)語法 ¶

Class definition 最簡單的形式如下:

Class definition,如同函式定義( def 陳述式),必須在它們有任何效果前先執行。(你可以想像把 class definition 放在一個 if 陳述式的分支,或在函式裡。)

在實作時,class definition 內的陳述式通常會是函式定義,但其他陳述式也是允許的,有時很有用——我們稍後會回到這裡。Class 中的函式定義通常會有一個獨特的引數列表形式,取決於 method 的呼叫慣例——再一次地,這將會在稍後解釋。

當進入 class definition,一個新的命名空間將會被建立,並且作為區域作用域——因此,所有區域變數的賦值將進入這個新的命名空間。特別是,函式定義會在這裡連結新函式的名稱。

正常地(從結尾處)離開 class definition 時,一個 class 物件 會被建立。基本上這是一個包裝器 (wrapper),裝著 class definition 建立的命名空間內容;我們將在下一節中更加了解 class 物件。原始的區域作用域(在進入 class definition 之前已生效的作用域)會恢復,在此 class 物件會被連結到 class definition 標頭中給出的 class 名稱(在範例中為 ClassName )。

9.3.2. Class 物件 ¶

Class 物件支援兩種運算:屬性參照 (attribute reference) 和實例化 (instantiation)。

屬性參照 使用 Python 中所有屬性參照的標準語法: obj.name 。有效的屬性名稱是 class 物件被建立時,class 的命名空間中所有的名稱。所以,如果 class definition 看起來像這樣:

那麼 MyClass.i 和 MyClass.f 都是有效的屬性參照,會分別回傳一個整數和一個函式物件。Class 屬性也可以被指派 (assign),所以您可以透過賦值改變 MyClass.i 的值。 __doc__ 也是一個有效的屬性,會回傳屬於該 class 的說明字串 (docstring): "A simple example class" 。

Class 實例化 使用了函式記法 (function notation)。就好像 class 物件是一個沒有參數的函式,它回傳一個新的 class 實例。例如(假設是上述的 class):

建立 class 的一個新 實例 ,並將此物件指派給區域變數 x 。

實例化運算(「呼叫」一個 class 物件)會建立一個空的物件。許多 class 喜歡在建立物件時有著自訂的特定實例初始狀態。因此,class 可以定義一個名為 __init__() 的特別 method,像這樣:

當 class 定義了 __init__() method,class 實例化會為新建的 class 實例自動調用 __init__() 。所以在這個範例中,一個新的、初始化的實例可以如此獲得:

當然, __init__() method 可能為了更多的彈性而有引數。在這種情況下,要給 class 實例化運算子的引數會被傳遞給 __init__() 。例如:

9.3.3. 實例物件 ¶

現在,我們可以如何處理實例物件?實例物件能理解的唯一運算就是屬性參照。有兩種有效的屬性名稱:資料屬性 (data attribute) 和 method。

資料屬性 對應 Smalltalk 中的「實例變數」,以及 C++ 中的「資料成員」。資料屬性不需要被宣告;和區域變數一樣,它們在第一次被賦值時就會立即存在。例如,如果 x 是 MyClass 在上述例子中建立的實例,下面的程式碼將印出值 16 ,而不留下蹤跡:

實例的另一種屬性參照是 method 。Method 是一個「屬於」物件的函式。(在 Python 中,術語 method 並不是 class 實例所獨有的:其他物件型別也可以有 method。例如,list 物件具有稱為 append、insert、remove、sort 等 method。但是,在下面的討論中,我們將用術語 method 來專門表示 class 實例物件的 method,除非另有明確說明。)

實例物件的有效 method 名稱取決於其 class。根據定義,一個 class 中所有的函式物件屬性,就定義了實例的對應 method。所以在我們的例子中, x.f 是一個有效的 method 參照,因為 MyClass.f 是一個函式,但 x.i 不是,因為 MyClass.i 不是。但 x.f 與 MyClass.f 是不一樣的——它是一個 method 物件 ,而不是函式物件。

9.3.4. Method 物件 ¶

通常,一個 method 在它被連結後隨即被呼叫:

在 MyClass 的例子中,這將回傳字串 'hello world' 。然而,並沒有必要立即呼叫一個 method: x.f 是一個 method 物件,並且可以被儲藏起來,之後再被呼叫。舉例來說:

將會持續印出 hello world 直到天荒地老。

當一個 method 被呼叫時究竟會發生什麼事?你可能已經注意到 x.f() 被呼叫時沒有任何的引數,儘管 f() 的函式定義有指定一個引數。這個引數發生了什麼事?當一個需要引數的函式被呼叫而沒有給任何引數時,Python 肯定會引發例外——即使該引數實際上沒有被使用...

事實上,你可能已經猜到了答案:method 的特殊之處在於,實例物件會作為函式中的第一個引數被傳遞。在我們的例子中, x.f() 這個呼叫等同於 MyClass.f(x) 。一般來說,呼叫一個有 n 個引數的 method,等同於呼叫一個對應函式,其引數列表 (argument list) 被建立時,會在第一個引數前插入該 method 的實例物件。

一般來說,方法的工作原理如下。當一個實例的非資料屬性被參照時,將會搜尋該實例的 class。如果該名稱是一個有效的 class 屬性,而且是一個函式物件,則對實例物件和函式物件的參照都會被打包到方法物件中。當使用引數串列呼叫方法物件時,會根據實例物件和引數串列來建構一個新的引數串列,並使用該新引數串列來呼叫函式物件。

9.3.5. Class 及實例變數 ¶

一般來說,實例變數用於每一個實例的獨特資料,而 class 變數用於該 class 的所有實例共享的屬性和 method:

如同在 關於名稱與物件的一段話 的討論,共享的資料若涉及 mutable 物件,如 list 和 dictionary,可能會產生意外的影響。舉例來說,下列程式碼的 tricks list 不應該作為一個 class 變數使用,因為這個 list 將會被所有的 Dog 實例所共享:

正確的 class 設計應該使用實例變數:

9.4. 隨意的備註 ¶

如果屬性名稱同時出現在一個實例和一個 class 中,則屬性的尋找會以實例為優先:

資料屬性可能被 method 或是被物件的一般使用者(「客戶端」)所參照。也就是說,class 不可用於實作純粹抽象的資料型別。事實上,在 Python 中沒有任何可能的方法,可強制隱藏資料——這都是基於慣例。(另一方面,以 C 編寫的 Python 實作可以完全隱藏實作細節並且在必要時控制物件的存取;這可以被以 C 編寫的 Python 擴充所使用。)

客戶端應該小心使用資料屬性——客戶端可能會因為覆寫他們的資料屬性,而破壞了被 method 維護的不變性。注意,客戶端可以增加他們自己的資料屬性到實例物件,但不影響 method 的有效性,只要避免名稱衝突即可——再一次提醒,命名慣例可以在這裡節省很多麻煩。

在 method 中參照資料屬性(或其他 method!)是沒有簡寫的。我發現這實際上增加了 method 的可閱讀性:在瀏覽 method 時,絕不會混淆區域變數和實例變數。

通常,方法的第一個引數稱為 self 。這僅僅只是一個慣例: self 這個名字對 Python 來說完全沒有特別的意義。但請注意,如果不遵循慣例,你的程式碼可能對其他 Python 程式設計師來說可讀性較低,此外,也可以想像一個可能因信任此慣例而編寫的 class 瀏覽器 (browser) 程式。

任何一個作為 class 屬性的函式物件都為該 class 的實例定義了一個相應的 method。函式定義不一定要包含在 class definition 的文本中:將函式物件指定給 class 中的區域變數也是可以的。例如:

現在 f 、 g 和 h 都是 class C 的屬性,並指向函式物件,所以他們都是class C 實例的 method —— h 與 g 是完全一樣的。請注意,這種做法通常只會使該程式的讀者感到困惑。

Method 可以藉由使用 self 引數的 method 屬性,呼叫其他 method:

Method 可以用與一般函式相同的方式參照全域名稱。與 method 相關的全域作用域,就是包含其定義的模組。(class 永遠不會被用作全域作用域。)雖然人們很少有在 method 中使用全域資料的充分理由,但全域作用域仍有許多合法的使用:比方說,被 import 至全域作用域的函式和模組,可以被 method 以及在該作用域中定義的函式和 class 所使用。通常,包含 method 的 class,它本身就是被定義在這個全域作用域,在下一節,我們將看到 method 想要參照自己的 class 的一些好原因。

每個值都是一個物件,因此都具有一個 class ,也可以稱為它的 type(型別) 。它以 object.__class__ 被儲存。

9.5. 繼承 (Inheritance) ¶

當然,如果沒有支援繼承,「class」這個語言特色就不值得被稱為 class。一個 derived class(衍生類別)定義的語法看起來如下:

名稱 BaseClassName 必須被定義於作用域可及的命名空間,且該作用域要包含 derived class 定義。要代替 base class(基底類別)的名稱,用其他任意的運算式也是被允許的。這會很有用,例如,當一個 base class 是在另一個模組中被定義時:

執行 derived class 定義的過程,與執行 base class 相同。當 class 物件被建構時,base class 會被記住。這是用於解析屬性參照:如果一個要求的屬性無法在該 class 中找到,則會繼續在 base class 中搜尋。假如該 base class 本身也是衍生自其他 class,則這個規則會遞迴地被應用。

關於 derived class 的實例化並沒有特別之處: DerivedClassName() 會建立該 class 的一個新實例。Method 的參照被解析如下:對應的 class 屬性會被搜尋,如果需要,沿著 base class 的繼承鍊往下走,如果這產生了一個函式物件,則該 method 的參照是有效的。

Derived class 可以覆寫其 base class 的 method。因為 method 在呼叫同一個物件的其他 method 時沒有特別的特權,所以當 base class 的一個 method 在呼叫相同 base class 中定義的另一個 method 時,最終可能會呼叫到一個覆寫它的 derived class 中的 method。(給 C++ 程式設計師:Python 中所有 method 實際上都是 virtual 。)

一個在 derived class 覆寫的 method 可能事實上是想要擴充而非單純取代 base class 中相同名稱的 method。要直接呼叫 base class 的 method 有一個簡單的方法:只要呼叫 BaseClassName.methodname(self, arguments) 。這有時對客戶端也很有用。(請注意,只有在 base class 在全域作用域可以用 BaseClassName 被存取時,這方法才有效。)

Python 有兩個內建函式可以用於繼承:

使用 isinstance() 判斷一個實例的型別: isinstance(obj, int) 只有在 obj.__class__ 是 int 或衍伸自 int 時,結果才會是 True 。

使用 issubclass() 判斷 class 繼承: issubclass(bool, int) 會是 True ,因為 bool 是 int 的 subclass(子類別)。但是, issubclass(float, int) 是 False ,因為 float 並不是 int 的 subclass。

9.5.1. 多重繼承 ¶

Python 也支援多重繼承的形式。一個有多個 base class 的 class definition 看起來像這樣子:

在大多數情況下,最簡單的例子裡,你可以這樣思考,對於繼承自 parent class(父類別)的屬性,其搜尋規則為:深度優先、從左到右、在階層裡重疊的相同 class 中不重複搜尋。因此,假如有一個屬性在 DerivedClassName 沒有被找到,則在 Base1 搜尋它,接著(遞迴地)在 Base1 的 base class 中搜尋,假如在那裡又沒有找到的話,會在 Base2 搜尋,依此類推。

事實上,它稍微複雜一些;method 的解析順序是動態地變化,以支援對 super() 的合作呼叫。這個方式在其他的多重繼承語言中,稱為呼叫下一個方法 (call-next-method),且比在單一繼承語言中的 super call(超級呼叫)來得更強大。

動態排序是必要的,因為多重繼承的所有情況都表現一或多的菱形關係(其中至少一個 parent class 可以從最底層 class 透過多個路徑存取)。例如,所有的 class 都繼承自 object ,因此任何多重繼承的情況都提供了多個到達 object 的路徑。為了避免 base class 被多次存取,動態演算法以這些方式將搜尋順序線性化 (linearize):保留每個 class 中規定的從左到右的順序、對每個 parent 只會呼叫一次、使用單調的 (monotonic) 方式(意思是,一個 class 可以被 subclassed(子類別化),而不會影響其 parent 的搜尋優先順序)。總之,這些特性使設計出可靠又可擴充、具有多重繼承的 class 成為可能。更多資訊,請見 Python 2.3 方法解析顺序 。

9.6. 私有變數 ¶

「私有」(private) 實例變數,指的是不在物件內部便無法存取的變數,這在 Python 中是不存在的。但是,大多數 Python 的程式碼都遵守一個慣例:前綴為一個底線的名稱(如: _spam )應被視為 API (應用程式介面)的非公有 (non-public) 部分(無論它是函式、方法或是資料成員)。這被視為一個實作細節,如有調整,亦不另行通知。

既然 class 私有的成員已有一個有效的用例(即避免名稱與 subclass 定義的名稱衝突),這種機制也存在另一個有限的支援,稱為 name mangling (名稱修飾)。任何格式為 __spam (至少兩個前導下底線,最多一個尾隨下底線)的物件名稱 (identifier) 會被文本地被替換為 _classname__spam ,在此 classname 就是去掉前導下底線的當前 class 名稱。只要這個修飾是在 class 的定義之中發生,它就會在不考慮該物件名稱的語法位置的情況下完成。

名稱修飾對於讓 subclass 覆寫 method 而不用破壞 class 內部的 method 呼叫,是有幫助的。舉例來說:

在上例中,就算在 MappingSubclass 當中加入 __update 識別符,也能順利運作,因為在 Mapping class 中,它會被替換為 _Mapping__update ,而在 MappingSubclass class 中,它會被替換為 _MappingSubclass__update 。

請注意,修飾規則是被設計來避免意外;它仍可能存取或修改一個被視為私有的變數。這在特殊情況下甚至可能很有用,例如在除錯器 (debugger)。

另外也注意,傳遞給 exec() 或 eval() 的程式碼不會把調用 class 的名稱視為當前的 class;這和 global 陳述式的效果類似,該效果同樣僅限於整體被位元組編譯後 (byte-compiled) 的程式碼。同樣的限制適用於 getattr() , setattr() 和 delattr() ,以及直接參照 __dict__ 時。

9.7. 補充說明 ¶

如果有一種資料型別,類似於 Pascal 的「record」或 C 的「struct」,可以將一些有名稱的資料項目捆綁在一起,有時候這會很有用。符合語言習慣的做法是使用 dataclasses :

用來處理特殊抽象資料型別的一段 Python 程式碼,經常能以傳遞一個 class 來替代,此 class 模擬該資料型別的多種 method。例如,如果你有一個函式,它會從一個檔案物件來格式化某些資料,你也可以定義一個有 read() 和 readline() method 的 class 作為替代方式,從字串緩衝區取得資料,並將其作為引數來傳遞。

實例的 method 物件 也具有屬性: m.__self__ 就是帶有 method m() 的實例物件,而 m.__func__ 則是該 method 所對應的 函式物件 。

9.8. 疊代器 (Iterator) ¶

到目前為止,你可能已經注意到大多數的容器 (container) 物件都可以使用 for 陳述式來進行迴圈:

這種存取風格清晰、簡潔且方便。疊代器的使用在 Python 中處處可見且用法一致。在幕後, for 陳述式會在容器物件上呼叫 iter() 。該函式回傳一個疊代器物件,此物件定義了 __next__() method,而此 method 會逐一存取容器中的元素。當元素用盡時, __next__() 將引發 StopIteration 例外,來通知 for 終止迴圈。你可以使用內建函式 next() 來呼叫 __next__() method;這個例子展示了它的運作方式:

看過疊代器協定的幕後機制後,在你的 class 加入疊代器的行為就很容易了。定義一個 __iter__() method 來回傳一個帶有 __next__() method 的物件。如果 class 已定義了 __next__() ,則 __iter__() 可以只回傳 self :

9.9. 產生器 (Generator) ¶

產生器 是一個用於建立疊代器的簡單而強大的工具。它們的寫法和常規的函式一樣,但當它們要回傳資料時,會使用 yield 陳述式。每次在產生器上呼叫 next() 時,它會從上次離開的位置恢復執行(它會記得所有資料值以及上一個被執行的陳述式)。以下範例顯示,建立產生器可以相當地容易:

任何可以用產生器來完成的事,也能用以 class 為基礎的疊代器來完成,如同前一節的描述。而讓產生器的程式碼更為精簡的原因是, __iter__() 和 __next__() method 會自動被建立。

另一個關鍵的特性在於,區域變數和執行狀態會在每次呼叫之間自動被儲存。這使得該函式比使用 self.index 和 self.data 這種實例變數的方式更容易編寫且更為清晰。

除了會自動建立 method 和儲存程式狀態,當產生器終止時,它們還會自動引發 StopIteration 。這些特性結合在一起,使建立疊代器能與編寫常規函式一樣容易。

9.10. 產生器運算式 ¶

某些簡單的產生器可以寫成如運算式一般的簡潔程式碼,所用的語法類似 list comprehension(串列綜合運算),但外層為括號而非方括號。這種運算式被設計用於產生器將立即被外圍函式 (enclosing function) 所使用的情況。產生器運算式與完整的產生器定義相比,程式碼較精簡但功能較少,也比等效的 list comprehension 更為節省記憶體。

  • 9.1. 關於名稱與物件的一段話
  • 9.2.1. 作用域和命名空間的範例
  • 9.3.1. Class definition(類別定義)語法
  • 9.3.2. Class 物件
  • 9.3.3. 實例物件
  • 9.3.4. Method 物件
  • 9.3.5. Class 及實例變數
  • 9.5.1. 多重繼承
  • 9.8. 疊代器 (Iterator)
  • 9.9. 產生器 (Generator)
  • 9.10. 產生器運算式

10. Python 標準函式庫概覽

COMMENTS

  1. python

    The yield statement used in a function turns that function into a "generator" (a function that creates an iterator). The resulting iterator is normally resumed by calling next(). However it is possible to send values to the function by calling the method send() instead of next() to resume it: In your example this would assign the value 1 to c ...

  2. 7. Simple statements

    Annotation assignment is the combination, in a single statement, of a variable or attribute annotation and an optional assignment statement: annotated_assignment_stmt::= augtarget ":" expression ["=" (starred_expression | yield_expression)] The difference from normal Assignment statements is that only a single target is allowed.

  3. python

    26. The syntax variable = (yield some_value) in a generator does the following: it returns some_value to the code that invoked it (via next or send ); when it is next invoked (via .next or .send(another_value)) it assigns another_value to variable and continues execution. For example, suppose you have a generator function: >>> def f():

  4. How to Use Generators and yield in Python

    Python. pal_gen = infinite_palindromes() for i in pal_gen: digits = len(str(i)) pal_gen.send(10 ** (digits)) With this code, you create the generator object and iterate through it. The program only yields a value once a palindrome is found. It uses len() to determine the number of digits in that palindrome.

  5. Python Yield

    What the yield keyword does is as follows: Each time you iterate, Python runs the code until it encounters a yield statement inside the function. Then, it sends the yielded value and pauses the function in that state without exiting. When the function is invoked the next time, the state at which it was last paused is remembered and execution is ...

  6. Python's Assignment Operator: Write Robust Assignments

    Learning about the Python assignment operator and its use for writing assignment statements will arm you with powerful tools for writing better and more robust Python code. ... In this example, custom_reversed() is a generator function because it uses yield. Calling the function creates an iterator that yields items from the input iterable in ...

  7. Using Python Generators and yield: A Complete Guide • datagy

    Let's see how we can create a simple generator function: # Creating a Simple Generator Function in Python def return_n_values ( n ): num = 0 while num < n: yield num. num += 1. Let's break down what is happening here: We define a function, return_n_values(), which takes a single parameter, n. In the function, we first set the value of num to 0.

  8. Making sense of generators, coroutines, and "yield from" in Python

    By using "yield from", you not only outsource the yielded values to a sub-generator, but you also allow that sub-generator to get inputs from the user. For example: def switchboard(): choice = yield "Send 1 for Pig Latin, 2 for support". if choice == 1: yield from pig_latin_translator() elif choice == 2:

  9. Why "Yield" Is an Excellent Python Keyword

    We first define a coroutine function using the yield keyword. As you can see, the overall structure is similar to the generator function. However, you'll notice that the yield keyword appears after the assignment operator (i.e. the equals sign).; The assignment is possible because, in addition to the typical next() function that works with a generator, generators also support the send ...

  10. Understanding Yield Statement: A Deep Dive into Python's Generator

    The 'yield' keyword in Python is primarily used in the body of a function like a return statement, but it returns a generator object rather than a value. This generator object can be iterated over to retrieve potentially infinite values. This is the basis of generator functions. def count_up_to(n): count = 1.

  11. Understanding Python's "yield" Keyword

    Understanding Python's "yield" Keyword. The yield keyword in Python is used to create generators. A generator is a type of collection that produces items on-the-fly and can only be iterated once. By using generators you can improve your application's performance and consume less memory as compared to normal collections, so it provides a nice ...

  12. Understanding Python Yield: A Comprehensive Guide

    Now, let's dive into the 'yield from' construct. 'yield from' is a phrase used in Python to delegate part of the generator's operations to another generator or iterable. This can simplify the code in a generator function, particularly when it's dealing with nested for loops. def complex_generator(): yield from simple_generator() yield 4. yield 5.

  13. Python

    Python Yield It is generally used to convert a regular Python function into a generator. A generator is a special function in Python that returns a generator object to the caller. Since it stores the local variable states, hence overhead of memory allocation is controlled. Example: # Python3 code to demonstrate yield keyword # Use of yield def prin

  14. Python yield Keyword

    Definition and Usage. The yield keyword is used to return a list of values from a function. Unlike the return keyword which stops further execution of the function, the yield keyword continues to the end of the function. When you call a function with yield keyword (s), the return value will be a list of values, one for each yield.

  15. 8. Compound statements

    The starred_list expression is evaluated once; it should yield an iterable object. An iterator is created for that iterable. The first item provided by the iterator is then assigned to the target list using the standard rules for assignments (see Assignment statements), and the suite is executed. This repeats for each item provided by the iterator.

  16. Iterators and Iterables in Python: Run Efficient Iterations

    Work with iterators and iterables in your Python code. Use generator functions and the yield statement to create generator iterators. Build your own iterables using different techniques, such as the iterable protocol. Use the asyncio module and the await and async keywords to create asynchronous iterators.

  17. How to Use Yield Keyword for Memory Efficient Python Code

    In this article, we will see how to use the yield keyword for memory-efficient Python code. What is the Yield Keyword? The yield keyword in Python is used in the context of generator functions and is fundamental to creating iterators efficiently. It allows a function to pause its execution and yield a value to the caller without losing its state.

  18. PEP 572

    Unparenthesized assignment expressions are prohibited for the value of a keyword argument in a call. Example: foo(x = y := f(x)) # INVALID foo(x=(y := f(x))) # Valid, though probably confusing. This rule is included to disallow excessively confusing code, and because parsing keyword arguments is complex enough already.

  19. Python Operators

    x % 100 -> yield last two digits; Arithmetic Operators With Addition, Subtraction, Multiplication, Modulo and Power. Here is an example showing how different Arithmetic Operators in Python work: ... Assignment Operators in Python. Let's see an example of Assignment Operators in Python.

  20. Python: Understanding yield assignment in generator

    The generator's execution is suspended midway through execution of the line. (Remember that the value of a yield expression is not the same as the value it yields.) The x.send(10) call causes the yield expression to take value 10, and that value is what is assigned to message. Brilliant, that's the piece I was missing.

  21. Mastering Python: 7 Strategies for Writing Clear, Organized, and

    In the above function, the load_documents function uses the yield keyword. The method returns an object of type <class generator>. When we iterate over this object, it continues execution from where the last yield statement is. Therefore, a single document is loaded and processed, improving Python code efficiency. 4.

  22. Do assignments ever require locking?

    Assignments for mutable types/complex objects correspond to multiple byte code instructions. My understanding that a thread switch can occur between any two byte code instructions. I tried to find a good reference in the official Python docs. Best I could find was this: What kind of global mutation are thread-safe? which is rather fuzzy.

  23. Python yield multiple assignment

    Python: yield and yield assignment. 4. Return multiple values with generator expression. 4. Multiple yields in a generator expression? Python. 11. Assigning (yield) to a variable. 1. Chaining Nested Yields. 1. Python nested yield in for loop. 2. Yield in Python. Hot Network Questions Is entropy physical or idealistic?

  24. 9. Class(類別)

    請注意,區域賦值(預設情況)不會改變 scope_test 對 spam 的連結。 nonlocal 賦值改變了 scope_test 對 spam 的連結,而 global 賦值改變了模組層次的連結。. 你還可以發現,在 global 賦值之前,沒有對 spam 的連結。. 9.3. 初見 class¶. Class 採用一些新的語法,三個新的物件型別,以及一些新的語意。

  25. python

    16. yield is an expression. It used to be a statement, and it's most commonly used as an entire statement, but in Python 2.5, it was turned into an expression as part of new coroutine support. It's still commonly referred to as the "yield statement", partly due to outdated documentation and knowledge and partly because it's mostly used as a ...