• Free Python 3 Tutorial
  • Control Flow
  • Exception Handling
  • Python Programs
  • Python Projects
  • Python Interview Questions
  • Python Database
  • Data Science With Python
  • Machine Learning with Python

Related Articles

  • Solve Coding Problems
  • Variables under the hood in Python
  • Python None Keyword
  • How to install PIP3 in Linux?
  • How to write Comments in Python3?
  • Variable Shadowing in Python
  • What is the difference between Python's Module, Package and Library?
  • Working with Binary Data in Python
  • Usage of __main__.py in Python
  • Python2 vs Python3 | Syntax and performance Comparison
  • Primary and secondary prompt in Python
  • Internal working of Python
  • Check the equality of integer division and math.floor() of Regular division in Python
  • Memory Management in Python
  • Python - Iterate through list without using the increment variable
  • Ways to increment Iterator from inside the For loop in Python
  • What's new in Python 3.9?
  • Python Keywords and Identifiers
  • Python | Method Overloading
  • Nested-if statement in Python

Is Python call by reference or call by value

Python utilizes a system, which is known as “Call by Object Reference” or “Call by assignment”. If you pass arguments like whole numbers, strings , or tuples to a function, the passing is like a call-by-value because you can not change the value of the immutable objects being passed to the function. Passing mutable objects can be considered as call by reference or Python pass by assignment because when their values are changed inside the function, then it will also be reflected outside the function.

What is Call By Reference in Python?

In Python , “call by reference” is a way of handing arguments to functions where the reference to the real object gets passed instead of the object’s actual value. This means that if you make changes to the object within the function, those changes directly affect the original object outside the function. It’s important to highlight that this approach in Python is not the same as the traditional “call-by-reference” used in some other programming languages. Python follows its own model, often called “pass-by-object-reference,” emphasizing the passing of references to objects while maintaining its unique characteristics.

What is Call By Value in Python?

In Python, the common way of passing function arguments is through “call by value.” This means that when you pass a variable to a function, you’re essentially handing over the actual value of the variable itself, not the reference to the object it’s pointing to. Consequently, any changes made to the variable within the function don’t directly affect the original variable outside the function. This approach is consistent with the principles of “call by value” and sets it apart from the “call by reference” mechanisms seen in some other programming languages. In simpler terms, the function deals with a copy of the variable’s value, ensuring that the original variable remains unchanged in the broader context of the program.

Python Call By Reference or Call By Value

Below are some examples by which we can understand better about this:

Example 1: Call by Value in Python

In this example, the Python code demonstrates call by value behavior. Despite passing the string variable “Geeks” to the function, the local modification inside the function does not alter the original variable outside its scope, emphasizing the immutability of strings in Python.

Example 2:  Call by Reference in Python

In this example, the Python code illustrates call by reference behavior. The function add_more() modifies the original list mylist by appending an element, showcasing how changes made inside the function persist outside its scope, emphasizing the mutable nature of lists in Python.

Binding Names to Objects

Example 1: variable identity and object equality in python.

In Python, each variable to which we assign a value/container is treated as an object. When we are assigning a value to a variable, we are actually binding a name to an object .

Example 2: List Identity and Object Equality in Python

In this example, the Python code compares the memory addresses (locations) of two list variables, a and b , using the id() function. Despite having identical values, the is keyword evaluates to False , indicating that these lists are distinct objects in memory, highlighting the importance of memory address comparison for object identity in Python.

Example 3: Immutable Objects and Function Parameter Behavior in Python

In this example, the Python code demonstrates the immutability of strings by passing the variable string to the function foo() . Despite attempting to assign a new value inside the function, the original string remains unchanged outside its scope, illustrating that string variables are immutable in Python.

Example 4: Mutable Objects and In-Place Modification in Python Functions

When we pass a mutable variable into the function foo and modify it to some other name the function foo still points to that object and continue to point to that object during its execution. 

Please Login to comment...

  • python-basics
  • simmytarika5
  • shivangint2vjq

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

Pass-by-value, reference, and assignment | Pydon't 🐍

When you call a function in Python and give it some arguments... Are they passed by value? No! By reference? No! They're passed by assignment.

Python snippet containing the code `x is y`.

(If you are new here and have no idea what a Pydon't is, you may want to read the Pydon't Manifesto .)

Introduction

Many traditional programming languages employ either one of two models when passing arguments to functions:

  • some languages use the pass-by-value model; and
  • most of the others use the pass-by-reference model.

Having said that, it is important to know the model that Python uses, because that influences the way your code behaves.

In this Pydon't, you will:

  • see that Python doesn't use the pass-by-value nor the pass-by-reference models;
  • understand that Python uses a pass-by-assignment model;
  • learn about the built-in function id ;
  • create a better understanding for the Python object model;
  • realise that every object has 3 very important properties that define it;
  • understand the difference between mutable and immutable objects;
  • learn the difference between shallow and deep copies; and
  • learn how to use the module copy to do both types of object copies.

You can now get your free copy of the ebook “Pydon'ts – Write elegant Python code” on Gumroad .

Is Python pass-by-value?

In the pass-by-value model, when you call a function with a set of arguments, the data is copied into the function. This means that you can modify the arguments however you please and that you won't be able to alter the state of the program outside the function. This is not what Python does, Python does not use the pass-by-value model.

Looking at the snippet of code that follows, it might look like Python uses pass-by-value:

This looks like the pass-by-value model because we gave it a 3, changed it to a 4, and the change wasn't reflected on the outside ( a is still 3).

But, in fact, Python is not copying the data into the function.

To prove this, I'll show you a different function:

As we can see, the list l , that was defined outside of the function, changed after calling the function clearly_not_pass_by_value . Hence, Python does not use a pass-by-value model.

Is Python pass-by-reference?

In a true pass-by-reference model, the called function gets access to the variables of the callee! Sometimes, it can look like that's what Python does, but Python does not use the pass-by-reference model.

I'll do my best to explain why that's not what Python does:

If Python used a pass-by-reference model, the function would've managed to completely change the value of l outside the function, but that's not what happened, as we can see.

Let me show you an actual pass-by-reference situation.

Here's some Pascal code:

Look at the last lines of that code:

  • we assign 2 to x with x := 2 ;
  • we print x ;
  • we call foo with x as argument; and
  • we print x again.

What's the output of this program?

I imagine that most of you won't have a Pascal interpreter lying around, so you can just go to tio.run and run this code online

If you run this, you'll see that the output is

which can be rather surprising, if the majority of your programming experience is in Python!

The procedure foo effectively received the variable x and changed the value that it contained. After foo was done, the variable x (that lives outside foo ) had a different value. You can't do anything like this in Python.

Python object model

To really understand the way Python behaves when calling functions, it's best if we first understand what Python objects are, and how to characterise them.

The three characteristics of objects

In Python, everything is an object, and each object is characterised by three things:

  • its identity (an integer that uniquely identifies the object, much like social security numbers identify people);
  • a type (that identifies the operations you can do with your object); and
  • the object's content.

Here is an object and its three characteristics:

As we can see above, id is the built-in function you use to query the identity of an object, and type is the built-in function you use to query the type of an object.

(Im)mutability

The (im)mutability of an object depends on its type. In other words, (im)mutability is a characteristic of types, not of specific objects!

But what exactly does it mean for an object to be mutable? Or for an object to be immutable?

Recall that an object is characterised by its identity, its type, and its contents. A type is mutable if you can change the contents of its objects without changing its identity and its type.

Lists are a great example of a mutable data type. Why? Because lists are containers : you can put things inside lists and you can remove stuff from inside those same lists.

Below, you can see how the contents of the list obj change as we make method calls, but the identity of the list remains the same:

However, when dealing with immutable objects, it's a completely different story. If we check an English dictionary, this is what we get for the definition of “immutable”:

adjective: immutable – unchanging over time or unable to be changed.

Immutable objects' contents never change. Take a string as an example:

Strings are a good example for this discussion because, sometimes, they can look mutable. But they are not!

A very good indicator that an object is immutable is when all its methods return something. This is unlike list's .append method, for example! If you use .append on a list, you get no return value. On the other hand, whatever method you use on a string, the result is returned to you:

Notice how obj wasn't updated automatically to "HELLO, WORLD!" . Instead, the new string was created and returned to you.

Another great hint at the fact that strings are immutable is that you cannot assign to its indices:

This shows that, when a string is created, it remains the same. It can be used to build other strings, but the string itself always. stays. unchanged.

As a reference, int , float , bool , str , tuple , and complex are the most common types of immutable objects; list , set , and dict are the most common types of mutable objects.

Variable names as labels

Another important thing to understand is that a variable name has very little to do with the object itself.

In fact, the name obj was just a label that I decided to attach to the object that has identity 2698212637504, has the list type, and contents 1, 2, 3.

Just like I attached the label obj to that object, I can attach many more names to it:

Again, these names are just labels. Labels that I decided to stick to the same object. How can we know it's the same object? Well, all their “social security numbers” (the ids) match, so they must be the same object:

Therefore, we conclude that foo , bar , baz , and obj , are variable names that all refer to the same object.

The operator is

This is exactly what the operator is does: it checks if the two objects are the same .

For two objects to be the same, they must have the same identity:

It is not enough to have the same type and contents! We can create a new list with contents [1, 2, 3] and that will not be the same object as obj :

Think of it in terms of perfect twins. When two siblings are perfect twins, they look identical. However, they are different people!

Just as a side note, but an important one, you should be aware of the operator is not .

Generally speaking, when you want to negate a condition, you put a not in front of it:

So, if you wanted to check if two variables point to different objects, you could be tempted to write

However, Python has the operator is not , which is much more similar to a proper English sentence, which I think is really cool!

Therefore, the example above should actually be written

Python does a similar thing for the in operator, providing a not in operator as well... How cool is that?!

Assignment as nicknaming

If we keep pushing this metaphor forward, assigning variables is just like giving a new nickname to someone.

My friends from middle school call me “Rojer”. My friends from college call me “Girão”. People I am not close to call me by my first name – “Rodrigo”. However, regardless of what they call me, I am still me , right?

If one day I decide to change my haircut, everyone will see the new haircut, regardless of what they call me!

In a similar fashion, if I modify the contents of an object, I can use whatever nickname I prefer to see that those changes happened. For example, we can change the middle element of the list we have been playing around with:

We used the nickname foo to modify the middle element, but that change is visible from all other nicknames as well.

Because they all pointed at the same list object.

Python is pass-by-assignment

Having laid out all of this, we are now ready to understand how Python passes arguments to functions.

When we call a function, each of the parameters of the function is assigned to the object they were passed in. In essence, each parameter now becomes a new nickname to the objects that were given in.

Immutable arguments

If we pass in immutable arguments, then we have no way of modifying the arguments themselves. After all, that's what immutable means: “doesn't change”.

That is why it can look like Python uses the pass-by-value model. Because the only way in which we can have the parameter hold something else is by assigning it to a completely different thing. When we do that, we are reusing the same nickname for a different object:

In the example above, when we call foo with the argument 5 , it's as if we were doing bar = 5 at the beginning of the function.

Immediately after that, we have bar = 3 . This means “take the nickname "bar" and point it at the integer 3 ”. Python doesn't care that bar , as a nickname (as a variable name) had already been used. It is now pointing at that 3 !

Mutable arguments

On the other hand, mutable arguments can be changed. We can modify their internal contents. A prime example of a mutable object is a list: its elements can change (and so can its length).

That is why it can look like Python uses a pass-by-reference model. However, when we change the contents of an object, we didn't change the identity of the object itself. Similarly, when you change your haircut or your clothes, your social security number does not change:

Do you understand what I'm trying to say? If not, drop a comment below and I'll try to help.

Beware when calling functions

This goes to show you should be careful when defining your functions. If your function expects mutable arguments, you should do one of the two:

  • do not mutate the argument in any way whatsoever; or
  • document explicitly that the argument may be mutated.

Personally, I prefer to go with the first approach: to not mutate the argument; but there are times and places for the second approach.

Sometimes, you do need to take the argument as the basis for some kind of transformation, which would mean you would want to mutate the argument. In those cases, you might think about doing a copy of the argument (discussed in the next section), but making that copy can be resource intensive. In those cases, mutating the argument might be the only sensible choice.

Making copies

Shallow vs deep copies.

“Copying an object” means creating a second object that has a different identity (therefore, is a different object) but that has the same contents. Generally speaking, we copy one object so that we can work with it and mutate it, while also preserving the first object.

When copying objects, there are a couple of nuances that should be discussed.

Copying immutable objects

The first thing that needs to be said is that, for immutable objects, it does not make sense to talk about copies.

“Copies” only make sense for mutable objects. If your object is immutable, and if you want to preserve a reference to it, you can just do a second assignment and work on it:

Or, sometimes, you can just call methods and other functions directly on the original, because the original is not going anywhere:

So, we only need to worry about mutable objects.

Shallow copy

Many mutable objects can contain, themselves, mutable objects. Because of that, two types of copies exist:

  • shallow copies; and
  • deep copies.

The difference lies in what happens to the mutable objects inside the mutable objects.

Lists and dictionaries have a method .copy that returns a shallow copy of the corresponding object.

Let's look at an example with a list:

First, we create a list inside a list, and we copy the outer list. Now, because it is a copy , the copied list isn't the same object as the original outer list:

But if they are not the same object, then we can modify the contents of one of the lists, and the other won't reflect the change:

That's what we saw: we changed the first element of the copy_list , and the outer_list remained unchanged.

Now, we try to modify the contents of sublist , and that's when the fun begins!

When we modify the contents of sublist , both the outer_list and the copy_list reflect those changes...

But wasn't the copy supposed to give me a second list that I could change without affecting the first one? Yes! And that is what happened!

In fact, modifying the contents of sublist doesn't really modify the contents of neither copy_list nor outer_list : after all, the third element of both was pointing at a list object, and it still is! It's the (inner) contents of the object to which we are pointing that changed.

Sometimes, we don't want this to happen: sometimes, we don't want mutable objects to share inner mutable objects.

Common shallow copy techniques

When working with lists, it is common to use slicing to produce a shallow copy of a list:

Using the built-in function for the respective type, on the object itself, also builds shallow copies. This works for lists and dictionaries, and is likely to work for other mutable types.

Here is an example with a list inside a list:

And here is an example with a list inside a dictionary:

When you want to copy an object “thoroughly”, and you don't want the copy to share references to inner objects, you need to do a “deep copy” of your object. You can think of a deep copy as a recursive algorithm.

You copy the elements of the first level and, whenever you find a mutable element on the first level, you recurse down and copy the contents of those elements.

To show this idea, here is a simple recursive implementation of a deep copy for lists that contain other lists:

We can use this function to copy the previous outer_list and see what happens:

As you can see here, modifying the contents of sublist only affected outer_list indirectly; it didn't affect copy_list .

Sadly, the list_deepcopy method I implemented isn't very robust, nor versatile, but the Python Standard Library has got us covered!

The module copy and the method deepcopy

The module copy is exactly what we need. The module provides two useful functions:

  • copy.copy for shallow copies; and
  • copy.deepcopy for deep copies.

And that's it! And, what is more, the method copy.deepcopy is smart enough to handle issues that might arise with circular definitions, for example! That is, when an object contains another that contains the first one: a naïve recursive implementation of a deep copy algorithm would enter an infinite loop!

If you write your own custom objects and you want to specify how shallow and deep copies of those should be made, you only need to implement __copy__ and __deepcopy__ , respectively!

It's a great module, in my opinion.

Examples in code

Now that we have gone deep into the theory – pun intended –, it is time to show you some actual code that plays with these concepts.

Mutable default arguments

Let's start with a Twitter favourite:

Python 🐍 is an incredible language but sometimes appears to have quirks 🤪 For example, one thing that often confuses beginners is why you shouldn't use lists as default values 👇 Here is a thread 👇🧵 that will help you understand this 💯 pic.twitter.com/HVhPjS2PSH — Rodrigo 🐍📝 (@mathsppblog) October 5, 2021

Apparently, it's a bad idea to use mutable objects as default arguments. Here is a snippet showing you why:

The function above appends an element to a list and, if no list is given, appends it to an empty list by default.

Great, let's put this function to good use:

We use it once with 1 , and we get a list with the 1 inside. Then, we use it to append a 1 to another list we had. And finally, we use it to append a 3 to an empty list... Except that's not what happens!

As it turns out, when we define a function, the default arguments are created and stored in a special place:

What this means is that the default argument is always the same object. Therefore, because it is a mutable object, its contents can change over time. That is why, in the code above, __defaults__ shows a list with two items already.

If we redefine the function, then its __defaults__ shows an empty list:

This is why, in general, mutable objects shouldn't be used as default arguments.

The standard practice, in these cases, is to use None and then use Boolean short-circuiting to assign the default value:

With this implementation, the function now works as expected:

is not None

Searching through the Python Standard Library shows that the is not operator is used a bit over 5,000 times. That's a lot.

And, by far and large, that operator is almost always followed by None . In fact, is not None appears 3169 times in the standard library!

x is not None does exactly what it's written: it checks if x is None or not.

Here is a simple example usage of that, from the argparse module to create command line interfaces:

Even without a great deal of context, we can see what is happening: when displaying command help for a given section, we may want to indent it (or not) to show hierarchical dependencies.

If a section's parent is None , then that section has no parent, and there is no need to indent. In other words, if a section's parent is not None , then we want to indent it. Notice how my English matches the code exactly!

Deep copy of the system environment

The method copy.deepcopy is used a couple of times in the standard library, and here I'd like to show an example usage where a dictionary is copied.

The module os provides the attribute environ , similar to a dictionary, that contains the environment variables that are defined.

Here are a couple of examples from my (Windows) machine:

The module http.server provides some classes for basic HTTP servers.

One of those classes, CGIHTTPRequestHandler , implements a HTTP server that can also run CGI scripts and, in its run_cgi method, it needs to set a bunch of environment variables.

These environment variables are set to give the necessary context for the CGI script that is going to be ran. However, we don't want to actually modify the current environment!

So, what we do is create a deep copy of the environment, and then we modify it to our heart's content! After we are done, we tell Python to execute the CGI script, and we provide the altered environment as an argument.

The exact way in which this is done may not be trivial to understand. I, for one, don't think I could explain it to you. But that doesn't mean we can't infer parts of it:

Here is the code:

As we can see, we copied the environment and defined some variables. Finally, we created a new subprocess that gets the modified environment.

Here's the main takeaway of this Pydon't, for you, on a silver platter:

“ Python uses a pass-by-assignment model, and understanding it requires you to realise all objects are characterised by an identity number, their type, and their contents. ”

This Pydon't showed you that:

  • Python doesn't use the pass-by-value model, nor the pass-by-reference one;
  • Python uses a pass-by-assignment model (using “nicknames”);
  • its identity;
  • its type; and
  • its contents.
  • the id function is used to query an object's identifier;
  • the type function is used to query an object's type;
  • the type of an object determines whether it is mutable or immutable;
  • shallow copies copy the reference of nested mutable objects;
  • deep copies perform copies that allow one object, and its inner elements, to be changed without ever affecting the copy;
  • copy.copy and copy.deepcopy can be used to perform shallow/deep copies; and
  • you can implement __copy__ and __deepcopy__ if you want your own objects to be copiable.

If you prefer video content, you can check this YouTube video , which was inspired by this article.

If you liked this Pydon't be sure to leave a reaction below and share this with your friends and fellow Pythonistas. Also, subscribe to the newsletter so you don't miss a single Pydon't!

Take your Python 🐍 skills to the next level 🚀

I write about Python every week. Join +16.000 others who are taking their Python 🐍 skills to the next level 🚀, one email at a time.

  • Python 3 Docs, Programming FAQ, “How do I write a function with output parameters (call by reference)?”, https://docs.python.org/3/faq/programming.html#how-do-i-write-a-function-with-output-parameters-call-by-reference [last accessed 04-10-2021];
  • Python 3 Docs, The Python Standard Library, copy , https://docs.python.org/3/library/copy.html [last accessed 05-10-2021];
  • effbot.org, “Call by Object” (via “arquivo.pt”), https://arquivo.pt/wayback/20160516131553/http://effbot.org/zone/call-by-object.htm [last accessed 04-10-2021];
  • effbot.org, “Python Objects” (via “arquivo.pt”), https://arquivo.pt/wayback/20191115002033/http://effbot.org/zone/python-objects.htm [last accessed 04-10-2021];
  • Robert Heaton, “Is Python pass-by-reference or pass-by-value”, https://robertheaton.com/2014/02/09/pythons-pass-by-object-reference-as-explained-by-philip-k-dick/ [last accessed 04-10-2021];
  • StackOverflow question and answers, “How do I pass a variable by reference?”, https://stackoverflow.com/q/986006/2828287 [last accessed 04-10-2021];
  • StackOverflow question and answers, “Passing values in Python [duplicate]”, https://stackoverflow.com/q/534375/2828287 [last accessed 04-10-2021];
  • Twitter thread by @mathsppblog , https://twitter.com/mathsppblog/status/1445148566721335298 [last accessed 20-10-2021];

Previous Post Next Post

Random Article

Stay in the loop, popular tags.

  • 1 February 2024
  • 8 January 2024
  • 1 December 2023
  • 22 November 2023
  • 4 October 2023
  • 6 September 2023
  • 7 August 2023
  • 12 July 2023
  • 4 June 2023
  • 5 April 2023
  • 8 March 2023

Why is 6 afraid of 7? Because 7 8 9.

mathspp

avatar

Python Course #12: Pass by Assignment, Copy, Reference, and None

Now that the you know what mutable (e.g. lists , sets and dictionaries ) and immutable data types (e.g. tuples ) are it is important to talk about copies, references and None .

Python Pass by Assignment

When you call a pass data to a function such as print(...) there are two ways. The first option is to pass the data directly as a value:

The second option is to pass the data as a variable:

The difference between those two options is that the second option allows you to access the data you passed to the function later in your program by stating the variable. When the function that receives the data modifies it internally, you have to be aware of two critical things:

When a primitive data type ( bool , int , float , and str ) or immutable data type (e.g. tuple ) is passed to a function, its value is copied. Therefore, all changes made inside a function won’t affect the values stored in the variables passed to the function. This is called pass by value .

When a mutable data type (e.g. list , set , dict ) is passed to a function, the data isn’t copied. Because of this, changes that are made inside the function affect the values outside of the function. This is called pass by reference

This sounds pretty theoretical, so here comes an example:

Even though you haven’t learned about declaring functions in Python this example declares the function func(x, y) that takes two parameters. func(x, y) subtracts 1 from the first parameter, and calls pop() on the second one. That is everything you need to understand at the moment.

In the main program, the int i and the list l are declared and then passed to func(x, y) . When looking at i and l after func(x, y) has been executed, you can see that i is still 1 and not 0 because i ’s value was copied. However, l is missing its last element since it was passed as a reference.

This mix of pass by value and pass by reference in Python is called pass by assignment . It is essential to keep this concept always in mind when writing Python code.

In the previous parts of this Python course, you have seen that it is possible to get a copy of a mutable data type by calling the .copy() function:

By using .copy() , the data outside of the function isn’t changed.

In the following example the difference between a copy and reference is further amplified:

In the first line the list ['a', 'b', 'c'] is declared and than a new reference to this list is created with l = . In the second line another reference to the list ['a', 'b', 'c'] is created with: m = l . In the third line the list is copied and a reference to that copy is created with: n = l.copy() .

There are three references to two lists with the same content. l and m reference the first list and n reference the second list . Because l and m reference the same list every modification done using l or m will always be reflected in both. n won’t change as it references a different list .

Python is vs ==

To check if two variables reference the same value you can use the is operator to compare the values use the == operator:

l is n evaluates to False because they reference differnt list s, however, those two list s contain the same values and therefore l == n evaluates to True . You can also check the id that a variable is referencing using id(...) :

You can see that the id of l and m is the same and the id of n is different (The numbers are different everytime you run this code). The is operator is actually implemented using == with id(...) == id(...) .

Python None

Now that you know the difference between a copy and a reference, there is one last thing to talk about: ` None . The keyword None in Python indicates no value at all. And there is only one None`:

None can be used to initialize a variable without assigning a value. This can be useful when the value of a variable is assigned under a condition:

None actually has it’s own type and is an immutable data type because it can’t be changed:

None concludes this article. Throughout this Python course, you will come across pass by assignment, copies, references, and None reasonably often. Make sure to get the free Python Cheat Sheets in my Gumroad shop . If you have any questions about this article, feel free to join our Discord community to ask them over there.

Further Reading

Big o notation explained.

Why is Big O Notation Used? When you got different algorithms to solve the same problem, you need to compare those to each other to pick the best (meaning fastest) for your program. Looking at eac...

Python Course #2: Variables and Primitive Datatypes for Absolute Beginners

After you have written your first Python Hello World program (find the article over here: Python Lesson 1: Your First Python Program (Complete Beginners Guide) ) it is time to talk about variables ...

Python Course #3: Introduction to Boolean Algebra

The term Boolean algebra itself might sound dry and dull, but it is one of the backbones of all the computers we use today. In this article, you will learn what Boolean algebra is and how to use it...

Estimate Gas in Ethereum Transactions with Python web3 and brownie

Change Windows 10/11 Display Resolution with a Python System Tray Application

Trending Tags

python assignment by value or reference

PYTHON PROGRAMMER

Everything i know about python..., learn to write pythonic code.

Check out the book Writing Idiomatic Python !

Looking for Python Tutoring? Remote and local (NYC) slots still available! Email me at [email protected] for more info.

On line 1, we create a binding between a name , some_guy , and a string object containing 'Fred'. In the context of program execution, the environment is altered; a binding of the name some_guy ' to a string object is created in the scope of the block where the statement occurred. When we later say some_guy = 'George' , the string object containing 'Fred' is unaffected. We've just changed the binding of the name some_guy . We haven't, however, changed either the 'Fred' or 'George' string objects . As far as we're concerned, they may live on indefinitely.

With only a single name binding, this may seem overly pedantic, but it becomes more important when bindings are shared and function calls are involved. Let's say we have the following bit of Python code:

So what get's printed in the final line? Well, to start, the binding of some_guy to the string object containing 'Fred' is added to the block's namespace . The name first_names is bound to an empty list object. On line 4, a method is called on the list object first_names is bound to, appending the object some_guy is bound to. At this point, there are still only two objects that exist: the string object and the list object. some_guy and first_names[0] both refer to the same object (Indeed, print(some_guy is first_names[0]) shows this).

Let's continue to break things down. On line 6, a new name is bound: another_list_of_names . Assignment between names does not create a new object. Rather, both names are simply bound to the same object. As a result, the string object and list object are still the only objects that have been created by the interpreter. On line 7, a member function is called on the object another_list_of_names is bound to and it is mutated to contain a reference to a new object: 'George'. So to answer our original question, the output of the code is

This brings us to an important point: there are actually two kinds of objects in Python. A mutable object exhibits time-varying behavior. Changes to a mutable object are visible through all names bound to it. Python's lists are an example of mutable objects. An immutable object does not exhibit time-varying behavior. The value of immutable objects can not be modified after they are created. They can be used to compute the values of new objects, which is how a function like string.join works. When you think about it, this dichotomy is necessary because, again, everything is an object in Python. If integers were not immutable I could change the meaning of the number '2' throughout my program.

It would be incorrect to say that "mutable objects can change and immutable ones can't", however. Consider the following:

Tuples in Python are immutable. We can't change the tuple object name_tuple is bound to. But immutable containers may contain references to mutable objects like lists. Therefore, even though name_tuple is immutable, it "changes" when 'Igor' is appended to first_names on the last line. It's a subtlety that can sometimes (though very infrequently) prove useful.

By now, you should almost be able to intuit how function calls work in Python. If I call foo(bar) , I'm merely creating a binding within the scope of foo to the object the argument bar is bound to when the function is called. If bar refers to a mutable object and foo changes its value, then these changes will be visible outside of the scope of the function.

On the other hand, if bar refers to an immutable object, the most that foo can do is create a name bar in its local namespace and bind it to some other object.

Hopefully by now it is clear why Python is neither "call-by-reference" nor "call-by-value". In Python a variable is not an alias for a location in memory. Rather, it is simply a binding to a Python object. While the notion of "everything is an object" is undoubtedly a cause of confusion for those new to the language, it allows for powerful and flexible language constructs, which I'll discuss in my next post.

« Starting a Django 1.4 Project the Right Way

Like this article?

Why not sign up for Python Tutoring ? Sessions can be held remotely using Google+/Skype or in-person if you're in the NYC area. Email [email protected] if interested.

Sign up for the free jeffknupp.com email newsletter. Sent roughly once a month, it focuses on Python programming, scalable web development, and growing your freelance consultancy. And of course, you'll never be spammed, your privacy is protected, and you can opt out at any time.

Python pass by reference or value with examples

In this Python tutorial , let us discuss on Python pass by reference or value with a few examples.

  • Python pass by reference vs pass by value
  • Python call by reference vs call by value
  • Python pass by reference example
  • Python pass by value example
  • Pass by reference vs value in python
  • Python function arguments pass by reference or value
  • Python pass string by value

Table of Contents

Pass by reference – It is used in some programming languages, where values to the argument of the function are passed by reference which means that the address of the variable is passed and then the operation is done on the value stored at these addresses.

Pass by value – It means that the value is directly passed as the value to the argument of the function. Here, the operation is done on the value and then the value is stored at the address. Pass by value is used for a copy of the variable.

Call by reference vs call by value

Read: Python NumPy linspace

When we pass something by reference any change we make to the variable inside the function then those changes are reflected to the outside value as well.

After writing the above code, Once you will print  “student”  then the output will appear. Here, we created a dictionary called student , and test(student) is the function. Then two more students joined so we created the variable as “new” and the student.update(new) is used to update the dictionary, also the print will display the output.

You can refer to the below screenshot for python pass by reference example

Python pass by reference or value

When we pass something by value then the changes made to the function or copying of the variable are not reflected back to the calling function.

After writing the above code, Once you will print  “student”  then the output will appear. Here, we created a dictionary called student , and test(student) is the function. Then two more students joined so we created the variable as “new” and the print will display the output. We can see that the inside and outside function remains the same.

You can refer to the below screenshot for the python pass by value example

Python pass by value example

In the below example, we can see that all the parameters in the python language are passed by reference. So, if we change what a parameter refers to within a function, the change also reflects back in the calling function.

In this output, we can see that we are maintaining the reference of the passed object, and values are appending in the same object. So, you can see the output of the inside function and outside function.

You can refer to the below screenshot pass by reference vs value in python

Pass by reference vs value in python

The parameters in the python language are passed by reference. Which mean if we change what parameter refers to within the function, the change also reflect black in the calling function.

After writing the above code, Once you will print  “teacher”  then the output will appear. Here, we created a dictionary called teacher , and the def test(teacher) is the function. Then two more teachers joined so we created the variable as “new” and the print will display the output. We can see that the inside and outside function remains the same.

You can refer to the below screenshot  python function arguments pass by reference or value .

Python function arguments pass by reference or value

Read: Python NumPy concatenate

In this example, we have passed strings to a function, and the string value is an immutable object which is being passed to the function. So, the changes made to the function or copying of the variable are not reflected back to the calling function.

In this output, once you will print  “my_string”  then the output will appear. Here, we created the function called def test(my_string) . Here, the passing is like a pass string by the value as we can not change the value of the immutable object.

You can refer to the below screenshot python pass string by value .

Python pass string by value

You may like the following Python tutorials:

  • Python select from a list + Examples
  • Python Tkinter Listbox – How to Use
  • Python copy file (Examples)
  • Python File methods (With Useful Examples)
  • Python tkinter messagebox + Examples
  • Union of sets Python + Examples
  • How to convert a String to DateTime in Python
  • Escape sequence in Python

In this Python tutorial, we have learned about the python pass by reference or value . Also, We covered these below topics:

Bijay - Python Expert

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile .

All you need to know on by reference vs by value

by Szilard Magyar

gfKoji1a50mprGtRT9KEL4gPh6fNi6RGJ9CK

When it comes to software engineering there are quite a few misunderstood concepts and misused terms. By reference vs by value is definitely one of them.

I remember back in the day when I read up on the topic and every source I went through seemed to contradict the previous one. It took some time to get a solid grasp of it. I had no choice as it is a fundamental subject if you are a software engineer.

I ran into a nasty bug a few weeks back and decided to write an article so other people may have easier time figuring this whole thing out.

I code in Ruby on a daily basis. I also use JavaScript pretty often, so I have chosen these two languages for this presentation.

To understand all the concepts though we will use some Go and Perl examples as well.

To grasp the whole topic you have to understand 3 different things:

  • How the underlying data structures are implemented in the language (objects, primitive types, mutability,).
  • How variable assignment/copying/reassignment/comparison work

How variables are passed to functions

Underlying data types.

In Ruby there are no primitive types and everything is an object including integers and booleans.

And yes there is a TrueClass in Ruby.

These objects can be either mutable or immutable.

Immutable means there is no way you can change the object once it is created. There is just one instance for a given value with one object_id and it stays the same no matter what you do.

By default in Ruby the immutable object types are: Boolean , Numeric , nil , and Symbol .

In MRI the object_id of an object is the same as the VALUE that represents the object on the C level. For most kinds of objects this VALUE is a pointer to a location in memory where the actual object data is stored.

From now on we will use object_id and memory address interchangeably.

Let’s run some Ruby code in MRI for an immutable Symbol and a mutable String:

As you see while the symbol version keeps the same object_id for the same value, the string values belong to different memory addresses.

Unlike Ruby, JavaScript has primitive types.

They are — Boolean , null , undefined , String , and Number .

The rest of the data types go under the umbrella of Objects ( Array , Function , and Object) . There is nothing fancy here it is way more straightforward than Ruby.

Variable assignment , copying , reassignment and comparison

In Ruby every variable is just a reference to an object (since everything is an object).

When you assign a variable, it is a reference to an object not the object itself. When you copy an object b = a both variables will point to the same address.

This behavior is called copy by reference value .

Strictly speaking in Ruby and JavaScript everything is copied by value.

When it comes to objects though, the values happen to be the memory addresses of those objects. Thanks to this we can modify values that sit in those memory addresses. Again, this is called copy by reference value but most people refer to this as copy by reference.

It would be copy by reference if after reassigning a to ‘new string’, b would also point to the same address and have the same ‘new string’ value.

EDUJJ3qKxslYQaQU4HGqSfr4CJH5gwgu2e10

The same with an immutable type like Integer:

When you reassign a to the same integer, the memory address stays the same since a given integer always has the same object_id.

As you see when you compare any object to another one it is compared by value. If you wanna check out if they are the same object you have to use object_id.

Let’s see the JavaScript version:

Except the comparison — JavaScript uses by value for primitive types and by reference for objects. The behavior looks to be the same just like in Ruby.

Well, not quite.

Primitive values in JavaScript will not be shared between multiple variables . Even if you set the variables equal to each other. Every variable representing a primitive value is guaranteed to belong to a unique memory location.

This means none of the variables will ever point to the same memory address. It is also important that the value itself is stored in a physical memory location.

In our example when we declare b = a , b will point to a different memory address with the same ‘string’ value right away. So you don’t need to reassign a to point to a different memory address.

This is called copied by value since you have no access to the memory address only to the value.

-KYjFr8QIDdsGNMvjrsUac-V5KI6soar-ex3

Let’s see a better example where all this matters.

In Ruby if we modify the value that sits in the memory address then all the references that point to the address will have the same updated value:

You might think in JavaScript only the value of a would change but no. You can’t even change the original value as you don’t have direct access to the memory address.

You could say you assigned ‘x’ to a but it was assigned by value so a ’s memory address holds the value ‘x’, but you can’t change it as you have no reference to it.

The behavior of JavaScript objects and implementation are the same like Ruby’s mutable objects. Both copy be reference value.

JavaScript primitive types are copied by value. The behavior is the same like Ruby’s immutable objects which are copied by reference value .

Again, when you copy something by value it means you can’t change (mutate) the original value since there is no reference to the memory address. From the perspective of the writing code this is the same thing like having immutable entities that you can’t mutate.

If you compare Ruby and JavaScript the only data type that ‘behaves’ differently by default is String (that’s why we used String in the examples above).

In Ruby it is a mutable object and it is copied/passed by reference value while in JavaScript it is a primitive type and copied/passed by value.

When you wanna clone (not copy) an object you have to do it explicitly in both languages so you can make sure the original object won’t be modified:

It is crucial to remember this otherwise you will run into some nasty bugs when you invoke your code more than once. A good example would be a recursive function where you use the object as argument.

Another one is React (JavaScript front-end framework) where you always have to pass a new object for updating state as the comparison works based on object id.

This is faster because you don’t have to go through the object line by line to see if it has been changed.

Passing variables to functions is working the same way like copying for the same data types in most of the languages.

In JavaScript primitive types are copied and passed by value and objects are copied and passed by reference value.

I think this is the reason why people only talk about pass by value or pass by reference and never seem to mention copying. I guess they assume copying works the same way.

Now in JavaScript:

If you pass an object (not a primitive type like we did) in JavaScript to the function it works the same way like the Ruby example.

Other languages

We have already seen how copy/pass by value and copy/pass by reference value work. Now we will see what pass by reference is about and we also discover how we can change objects if we pass by value.

As I looked for pass by reference languages I couldn’t find too many and I ended up choosing Perl. Let’s see how copying works in Perl:

Well this seems to be the same just like in Ruby. I haven’t found any proof but I would say Perl is copied by reference value for String.

Now let’s check what pass by reference means:

Since Perl is passed by reference if you do a reassignment within the function it will change the original value of the memory address as well.

For pass by value language I have chosen Go as I intend to deepen my Go knowledge in the foreseeable future:

If you wanna change the value of a memory address you have to use pointers and pass around memory addresses by value. A pointer holds the memory address of a value.

The & operator generates a pointer to its operand and the * operator denotes the pointer’s underlying value. This basically means that you pass the memory address of a value with & and you set the value of a memory address with * .

How to evaluate a language:

  • Understand the underlying data types in the language. Read some specifications and play around with them. It usually boils down to primitive types and objects. Then check if those objects are mutable or immutable. Some languages use different copying/passing tactics for different data types.
  • Next step is the variable assignment, copying, reassignment and comparison. This is the most crucial part I think. Once you get this you will be able to figure out what’s going on. It helps a lot if you check the memory addresses when playing around.
  • Passing variables to functions usually is not special. It usually works the same way like copying in most languages. Once you you know how the variables are copied and reassigned you already know how they are passed to functions.

The languages we used here:

  • Go : Copied and passed by value
  • JavaScript : Primitive types are copied/passed by value, objects are copied/passed by reference value
  • Ruby : Copied and passed by reference value + mutable/immutable objects
  • Perl : Copied by reference value and passed by reference

When people say passed by reference they usually mean passed by reference value. Passing by reference value means that variables are passed around by value but those values are references to the objects .

As you saw Ruby only uses pass by reference value while JavaScript uses a mixed strategy. Still, the behavior is the same for almost all the data types due to the different implementation of the data structures.

Most of the mainstream languages are either copied and passed by value or copied and passed by reference value. For the last time: Pass by reference value is usually called pass by reference.

In general pass by value is safer as you won’t run into issues since you can’t accidentally change the original value. It is also slower to write because you have to use pointers if you want to change the objects.

It’s the same idea like with static typing vs dynamic typing — development speed at the cost of safety. As you guessed pass by value is usually a feature of lower level languages like C, Java or Go.

Pass by reference or reference value are usually used by higher level languages like JavaScript, Ruby and Python.

When you discover a new language go through the process like we did here and you will understand how it works.

This is not an easy topic and I am not sure everything is correct what I wrote here. If you think I have made some mistakes in this article, please let me know in the comments.

If this article was helpful, share it .

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

  • Python »
  • 3.12.2 Documentation »
  • Python Frequently Asked Questions »
  • Programming FAQ
  • Theme Auto Light Dark |

Programming FAQ ¶

General questions ¶, is there a source code level debugger with breakpoints, single-stepping, etc. ¶.

Several debuggers for Python are described below, and the built-in function breakpoint() allows you to drop into any of them.

The pdb module is a simple but adequate console-mode debugger for Python. It is part of the standard Python library, and is documented in the Library Reference Manual . You can also write your own debugger by using the code for pdb as an example.

The IDLE interactive development environment, which is part of the standard Python distribution (normally available as Tools/scripts/idle3 ), includes a graphical debugger.

PythonWin is a Python IDE that includes a GUI debugger based on pdb. The PythonWin debugger colors breakpoints and has quite a few cool features such as debugging non-PythonWin programs. PythonWin is available as part of pywin32 project and as a part of the ActivePython distribution.

Eric is an IDE built on PyQt and the Scintilla editing component.

trepan3k is a gdb-like debugger.

Visual Studio Code is an IDE with debugging tools that integrates with version-control software.

There are a number of commercial Python IDEs that include graphical debuggers. They include:

Are there tools to help find bugs or perform static analysis? ¶

Pylint and Pyflakes do basic checking that will help you catch bugs sooner.

Static type checkers such as Mypy , Pyre , and Pytype can check type hints in Python source code.

How can I create a stand-alone binary from a Python script? ¶

You don’t need the ability to compile Python to C code if all you want is a stand-alone program that users can download and run without having to install the Python distribution first. There are a number of tools that determine the set of modules required by a program and bind these modules together with a Python binary to produce a single executable.

One is to use the freeze tool, which is included in the Python source tree as Tools/freeze . It converts Python byte code to C arrays; with a C compiler you can embed all your modules into a new program, which is then linked with the standard Python modules.

It works by scanning your source recursively for import statements (in both forms) and looking for the modules in the standard Python path as well as in the source directory (for built-in modules). It then turns the bytecode for modules written in Python into C code (array initializers that can be turned into code objects using the marshal module) and creates a custom-made config file that only contains those built-in modules which are actually used in the program. It then compiles the generated C code and links it with the rest of the Python interpreter to form a self-contained binary which acts exactly like your script.

The following packages can help with the creation of console and GUI executables:

Nuitka (Cross-platform)

PyInstaller (Cross-platform)

PyOxidizer (Cross-platform)

cx_Freeze (Cross-platform)

py2app (macOS only)

py2exe (Windows only)

Are there coding standards or a style guide for Python programs? ¶

Yes. The coding style required for standard library modules is documented as PEP 8 .

Core Language ¶

Why am i getting an unboundlocalerror when the variable has a value ¶.

It can be a surprise to get the UnboundLocalError in previously working code when it is modified by adding an assignment statement somewhere in the body of a function.

works, but this code:

results in an UnboundLocalError :

This is because when you make an assignment to a variable in a scope, that variable becomes local to that scope and shadows any similarly named variable in the outer scope. Since the last statement in foo assigns a new value to x , the compiler recognizes it as a local variable. Consequently when the earlier print(x) attempts to print the uninitialized local variable and an error results.

In the example above you can access the outer scope variable by declaring it global:

This explicit declaration is required in order to remind you that (unlike the superficially analogous situation with class and instance variables) you are actually modifying the value of the variable in the outer scope:

You can do a similar thing in a nested scope using the nonlocal keyword:

What are the rules for local and global variables in Python? ¶

In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a value anywhere within the function’s body, it’s assumed to be a local unless explicitly declared as global.

Though a bit surprising at first, a moment’s consideration explains this. On one hand, requiring global for assigned variables provides a bar against unintended side-effects. On the other hand, if global was required for all global references, you’d be using global all the time. You’d have to declare as global every reference to a built-in function or to a component of an imported module. This clutter would defeat the usefulness of the global declaration for identifying side-effects.

Why do lambdas defined in a loop with different values all return the same result? ¶

Assume you use a for loop to define a few different lambdas (or even plain functions), e.g.:

This gives you a list that contains 5 lambdas that calculate x**2 . You might expect that, when called, they would return, respectively, 0 , 1 , 4 , 9 , and 16 . However, when you actually try you will see that they all return 16 :

This happens because x is not local to the lambdas, but is defined in the outer scope, and it is accessed when the lambda is called — not when it is defined. At the end of the loop, the value of x is 4 , so all the functions now return 4**2 , i.e. 16 . You can also verify this by changing the value of x and see how the results of the lambdas change:

In order to avoid this, you need to save the values in variables local to the lambdas, so that they don’t rely on the value of the global x :

Here, n=x creates a new variable n local to the lambda and computed when the lambda is defined so that it has the same value that x had at that point in the loop. This means that the value of n will be 0 in the first lambda, 1 in the second, 2 in the third, and so on. Therefore each lambda will now return the correct result:

Note that this behaviour is not peculiar to lambdas, but applies to regular functions too.

How do I share global variables across modules? ¶

The canonical way to share information across modules within a single program is to create a special module (often called config or cfg). Just import the config module in all modules of your application; the module then becomes available as a global name. Because there is only one instance of each module, any changes made to the module object get reflected everywhere. For example:

Note that using a module is also the basis for implementing the singleton design pattern, for the same reason.

What are the “best practices” for using import in a module? ¶

In general, don’t use from modulename import * . Doing so clutters the importer’s namespace, and makes it much harder for linters to detect undefined names.

Import modules at the top of a file. Doing so makes it clear what other modules your code requires and avoids questions of whether the module name is in scope. Using one import per line makes it easy to add and delete module imports, but using multiple imports per line uses less screen space.

It’s good practice if you import modules in the following order:

standard library modules – e.g. sys , os , argparse , re

third-party library modules (anything installed in Python’s site-packages directory) – e.g. dateutil , requests , PIL.Image

locally developed modules

It is sometimes necessary to move imports to a function or class to avoid problems with circular imports. Gordon McMillan says:

Circular imports are fine where both modules use the “import <module>” form of import. They fail when the 2nd module wants to grab a name out of the first (“from module import name”) and the import is at the top level. That’s because names in the 1st are not yet available, because the first module is busy importing the 2nd.

In this case, if the second module is only used in one function, then the import can easily be moved into that function. By the time the import is called, the first module will have finished initializing, and the second module can do its import.

It may also be necessary to move imports out of the top level of code if some of the modules are platform-specific. In that case, it may not even be possible to import all of the modules at the top of the file. In this case, importing the correct modules in the corresponding platform-specific code is a good option.

Only move imports into a local scope, such as inside a function definition, if it’s necessary to solve a problem such as avoiding a circular import or are trying to reduce the initialization time of a module. This technique is especially helpful if many of the imports are unnecessary depending on how the program executes. You may also want to move imports into a function if the modules are only ever used in that function. Note that loading a module the first time may be expensive because of the one time initialization of the module, but loading a module multiple times is virtually free, costing only a couple of dictionary lookups. Even if the module name has gone out of scope, the module is probably available in sys.modules .

Why are default values shared between objects? ¶

This type of bug commonly bites neophyte programmers. Consider this function:

The first time you call this function, mydict contains a single item. The second time, mydict contains two items because when foo() begins executing, mydict starts out with an item already in it.

It is often expected that a function call creates new objects for default values. This is not what happens. Default values are created exactly once, when the function is defined. If that object is changed, like the dictionary in this example, subsequent calls to the function will refer to this changed object.

By definition, immutable objects such as numbers, strings, tuples, and None , are safe from change. Changes to mutable objects such as dictionaries, lists, and class instances can lead to confusion.

Because of this feature, it is good programming practice to not use mutable objects as default values. Instead, use None as the default value and inside the function, check if the parameter is None and create a new list/dictionary/whatever if it is. For example, don’t write:

This feature can be useful. When you have a function that’s time-consuming to compute, a common technique is to cache the parameters and the resulting value of each call to the function, and return the cached value if the same value is requested again. This is called “memoizing”, and can be implemented like this:

You could use a global variable containing a dictionary instead of the default value; it’s a matter of taste.

How can I pass optional or keyword parameters from one function to another? ¶

Collect the arguments using the * and ** specifiers in the function’s parameter list; this gives you the positional arguments as a tuple and the keyword arguments as a dictionary. You can then pass these arguments when calling another function by using * and ** :

What is the difference between arguments and parameters? ¶

Parameters are defined by the names that appear in a function definition, whereas arguments are the values actually passed to a function when calling it. Parameters define what kind of arguments a function can accept. For example, given the function definition:

foo , bar and kwargs are parameters of func . However, when calling func , for example:

the values 42 , 314 , and somevar are arguments.

Why did changing list ‘y’ also change list ‘x’? ¶

If you wrote code like:

you might be wondering why appending an element to y changed x too.

There are two factors that produce this result:

Variables are simply names that refer to objects. Doing y = x doesn’t create a copy of the list – it creates a new variable y that refers to the same object x refers to. This means that there is only one object (the list), and both x and y refer to it.

Lists are mutable , which means that you can change their content.

After the call to append() , the content of the mutable object has changed from [] to [10] . Since both the variables refer to the same object, using either name accesses the modified value [10] .

If we instead assign an immutable object to x :

we can see that in this case x and y are not equal anymore. This is because integers are immutable , and when we do x = x + 1 we are not mutating the int 5 by incrementing its value; instead, we are creating a new object (the int 6 ) and assigning it to x (that is, changing which object x refers to). After this assignment we have two objects (the ints 6 and 5 ) and two variables that refer to them ( x now refers to 6 but y still refers to 5 ).

Some operations (for example y.append(10) and y.sort() ) mutate the object, whereas superficially similar operations (for example y = y + [10] and sorted(y) ) create a new object. In general in Python (and in all cases in the standard library) a method that mutates an object will return None to help avoid getting the two types of operations confused. So if you mistakenly write y.sort() thinking it will give you a sorted copy of y , you’ll instead end up with None , which will likely cause your program to generate an easily diagnosed error.

However, there is one class of operations where the same operation sometimes has different behaviors with different types: the augmented assignment operators. For example, += mutates lists but not tuples or ints ( a_list += [1, 2, 3] is equivalent to a_list.extend([1, 2, 3]) and mutates a_list , whereas some_tuple += (1, 2, 3) and some_int += 1 create new objects).

In other words:

If we have a mutable object ( list , dict , set , etc.), we can use some specific operations to mutate it and all the variables that refer to it will see the change.

If we have an immutable object ( str , int , tuple , etc.), all the variables that refer to it will always see the same value, but operations that transform that value into a new value always return a new object.

If you want to know if two variables refer to the same object or not, you can use the is operator, or the built-in function id() .

How do I write a function with output parameters (call by reference)? ¶

Remember that arguments are passed by assignment in Python. Since assignment just creates references to objects, there’s no alias between an argument name in the caller and callee, and so no call-by-reference per se. You can achieve the desired effect in a number of ways.

By returning a tuple of the results:

This is almost always the clearest solution.

By using global variables. This isn’t thread-safe, and is not recommended.

By passing a mutable (changeable in-place) object:

By passing in a dictionary that gets mutated:

Or bundle up values in a class instance:

There’s almost never a good reason to get this complicated.

Your best choice is to return a tuple containing the multiple results.

How do you make a higher order function in Python? ¶

You have two choices: you can use nested scopes or you can use callable objects. For example, suppose you wanted to define linear(a,b) which returns a function f(x) that computes the value a*x+b . Using nested scopes:

Or using a callable object:

In both cases,

gives a callable object where taxes(10e6) == 0.3 * 10e6 + 2 .

The callable object approach has the disadvantage that it is a bit slower and results in slightly longer code. However, note that a collection of callables can share their signature via inheritance:

Object can encapsulate state for several methods:

Here inc() , dec() and reset() act like functions which share the same counting variable.

How do I copy an object in Python? ¶

In general, try copy.copy() or copy.deepcopy() for the general case. Not all objects can be copied, but most can.

Some objects can be copied more easily. Dictionaries have a copy() method:

Sequences can be copied by slicing:

How can I find the methods or attributes of an object? ¶

For an instance x of a user-defined class, dir(x) returns an alphabetized list of the names containing the instance attributes and methods and attributes defined by its class.

How can my code discover the name of an object? ¶

Generally speaking, it can’t, because objects don’t really have names. Essentially, assignment always binds a name to a value; the same is true of def and class statements, but in that case the value is a callable. Consider the following code:

Arguably the class has a name: even though it is bound to two names and invoked through the name B the created instance is still reported as an instance of class A . However, it is impossible to say whether the instance’s name is a or b , since both names are bound to the same value.

Generally speaking it should not be necessary for your code to “know the names” of particular values. Unless you are deliberately writing introspective programs, this is usually an indication that a change of approach might be beneficial.

In comp.lang.python, Fredrik Lundh once gave an excellent analogy in answer to this question:

The same way as you get the name of that cat you found on your porch: the cat (object) itself cannot tell you its name, and it doesn’t really care – so the only way to find out what it’s called is to ask all your neighbours (namespaces) if it’s their cat (object)… ….and don’t be surprised if you’ll find that it’s known by many names, or no name at all!

What’s up with the comma operator’s precedence? ¶

Comma is not an operator in Python. Consider this session:

Since the comma is not an operator, but a separator between expressions the above is evaluated as if you had entered:

The same is true of the various assignment operators ( = , += etc). They are not truly operators but syntactic delimiters in assignment statements.

Is there an equivalent of C’s “?:” ternary operator? ¶

Yes, there is. The syntax is as follows:

Before this syntax was introduced in Python 2.5, a common idiom was to use logical operators:

However, this idiom is unsafe, as it can give wrong results when on_true has a false boolean value. Therefore, it is always better to use the ... if ... else ... form.

Is it possible to write obfuscated one-liners in Python? ¶

Yes. Usually this is done by nesting lambda within lambda . See the following three examples, slightly adapted from Ulf Bartelt:

Don’t try this at home, kids!

What does the slash(/) in the parameter list of a function mean? ¶

A slash in the argument list of a function denotes that the parameters prior to it are positional-only. Positional-only parameters are the ones without an externally usable name. Upon calling a function that accepts positional-only parameters, arguments are mapped to parameters based solely on their position. For example, divmod() is a function that accepts positional-only parameters. Its documentation looks like this:

The slash at the end of the parameter list means that both parameters are positional-only. Thus, calling divmod() with keyword arguments would lead to an error:

Numbers and strings ¶

How do i specify hexadecimal and octal integers ¶.

To specify an octal digit, precede the octal value with a zero, and then a lower or uppercase “o”. For example, to set the variable “a” to the octal value “10” (8 in decimal), type:

Hexadecimal is just as easy. Simply precede the hexadecimal number with a zero, and then a lower or uppercase “x”. Hexadecimal digits can be specified in lower or uppercase. For example, in the Python interpreter:

Why does -22 // 10 return -3? ¶

It’s primarily driven by the desire that i % j have the same sign as j . If you want that, and also want:

then integer division has to return the floor. C also requires that identity to hold, and then compilers that truncate i // j need to make i % j have the same sign as i .

There are few real use cases for i % j when j is negative. When j is positive, there are many, and in virtually all of them it’s more useful for i % j to be >= 0 . If the clock says 10 now, what did it say 200 hours ago? -190 % 12 == 2 is useful; -190 % 12 == -10 is a bug waiting to bite.

How do I get int literal attribute instead of SyntaxError? ¶

Trying to lookup an int literal attribute in the normal manner gives a SyntaxError because the period is seen as a decimal point:

The solution is to separate the literal from the period with either a space or parentheses.

How do I convert a string to a number? ¶

For integers, use the built-in int() type constructor, e.g. int('144') == 144 . Similarly, float() converts to floating-point, e.g. float('144') == 144.0 .

By default, these interpret the number as decimal, so that int('0144') == 144 holds true, and int('0x144') raises ValueError . int(string, base) takes the base to convert from as a second optional argument, so int( '0x144', 16) == 324 . If the base is specified as 0, the number is interpreted using Python’s rules: a leading ‘0o’ indicates octal, and ‘0x’ indicates a hex number.

Do not use the built-in function eval() if all you need is to convert strings to numbers. eval() will be significantly slower and it presents a security risk: someone could pass you a Python expression that might have unwanted side effects. For example, someone could pass __import__('os').system("rm -rf $HOME") which would erase your home directory.

eval() also has the effect of interpreting numbers as Python expressions, so that e.g. eval('09') gives a syntax error because Python does not allow leading ‘0’ in a decimal number (except ‘0’).

How do I convert a number to a string? ¶

To convert, e.g., the number 144 to the string '144' , use the built-in type constructor str() . If you want a hexadecimal or octal representation, use the built-in functions hex() or oct() . For fancy formatting, see the f-strings and Format String Syntax sections, e.g. "{:04d}".format(144) yields '0144' and "{:.3f}".format(1.0/3.0) yields '0.333' .

How do I modify a string in place? ¶

You can’t, because strings are immutable. In most situations, you should simply construct a new string from the various parts you want to assemble it from. However, if you need an object with the ability to modify in-place unicode data, try using an io.StringIO object or the array module:

How do I use strings to call functions/methods? ¶

There are various techniques.

The best is to use a dictionary that maps strings to functions. The primary advantage of this technique is that the strings do not need to match the names of the functions. This is also the primary technique used to emulate a case construct:

Use the built-in function getattr() :

Note that getattr() works on any object, including classes, class instances, modules, and so on.

This is used in several places in the standard library, like this:

Use locals() to resolve the function name:

Is there an equivalent to Perl’s chomp() for removing trailing newlines from strings? ¶

You can use S.rstrip("\r\n") to remove all occurrences of any line terminator from the end of the string S without removing other trailing whitespace. If the string S represents more than one line, with several empty lines at the end, the line terminators for all the blank lines will be removed:

Since this is typically only desired when reading text one line at a time, using S.rstrip() this way works well.

Is there a scanf() or sscanf() equivalent? ¶

Not as such.

For simple input parsing, the easiest approach is usually to split the line into whitespace-delimited words using the split() method of string objects and then convert decimal strings to numeric values using int() or float() . split() supports an optional “sep” parameter which is useful if the line uses something other than whitespace as a separator.

For more complicated input parsing, regular expressions are more powerful than C’s sscanf and better suited for the task.

What does ‘UnicodeDecodeError’ or ‘UnicodeEncodeError’ error mean? ¶

See the Unicode HOWTO .

Can I end a raw string with an odd number of backslashes? ¶

A raw string ending with an odd number of backslashes will escape the string’s quote:

There are several workarounds for this. One is to use regular strings and double the backslashes:

Another is to concatenate a regular string containing an escaped backslash to the raw string:

It is also possible to use os.path.join() to append a backslash on Windows:

Note that while a backslash will “escape” a quote for the purposes of determining where the raw string ends, no escaping occurs when interpreting the value of the raw string. That is, the backslash remains present in the value of the raw string:

Also see the specification in the language reference .

Performance ¶

My program is too slow. how do i speed it up ¶.

That’s a tough one, in general. First, here are a list of things to remember before diving further:

Performance characteristics vary across Python implementations. This FAQ focuses on CPython .

Behaviour can vary across operating systems, especially when talking about I/O or multi-threading.

You should always find the hot spots in your program before attempting to optimize any code (see the profile module).

Writing benchmark scripts will allow you to iterate quickly when searching for improvements (see the timeit module).

It is highly recommended to have good code coverage (through unit testing or any other technique) before potentially introducing regressions hidden in sophisticated optimizations.

That being said, there are many tricks to speed up Python code. Here are some general principles which go a long way towards reaching acceptable performance levels:

Making your algorithms faster (or changing to faster ones) can yield much larger benefits than trying to sprinkle micro-optimization tricks all over your code.

Use the right data structures. Study documentation for the Built-in Types and the collections module.

When the standard library provides a primitive for doing something, it is likely (although not guaranteed) to be faster than any alternative you may come up with. This is doubly true for primitives written in C, such as builtins and some extension types. For example, be sure to use either the list.sort() built-in method or the related sorted() function to do sorting (and see the Sorting Techniques for examples of moderately advanced usage).

Abstractions tend to create indirections and force the interpreter to work more. If the levels of indirection outweigh the amount of useful work done, your program will be slower. You should avoid excessive abstraction, especially under the form of tiny functions or methods (which are also often detrimental to readability).

If you have reached the limit of what pure Python can allow, there are tools to take you further away. For example, Cython can compile a slightly modified version of Python code into a C extension, and can be used on many different platforms. Cython can take advantage of compilation (and optional type annotations) to make your code significantly faster than when interpreted. If you are confident in your C programming skills, you can also write a C extension module yourself.

The wiki page devoted to performance tips .

What is the most efficient way to concatenate many strings together? ¶

str and bytes objects are immutable, therefore concatenating many strings together is inefficient as each concatenation creates a new object. In the general case, the total runtime cost is quadratic in the total string length.

To accumulate many str objects, the recommended idiom is to place them into a list and call str.join() at the end:

(another reasonably efficient idiom is to use io.StringIO )

To accumulate many bytes objects, the recommended idiom is to extend a bytearray object using in-place concatenation (the += operator):

Sequences (Tuples/Lists) ¶

How do i convert between tuples and lists ¶.

The type constructor tuple(seq) converts any sequence (actually, any iterable) into a tuple with the same items in the same order.

For example, tuple([1, 2, 3]) yields (1, 2, 3) and tuple('abc') yields ('a', 'b', 'c') . If the argument is a tuple, it does not make a copy but returns the same object, so it is cheap to call tuple() when you aren’t sure that an object is already a tuple.

The type constructor list(seq) converts any sequence or iterable into a list with the same items in the same order. For example, list((1, 2, 3)) yields [1, 2, 3] and list('abc') yields ['a', 'b', 'c'] . If the argument is a list, it makes a copy just like seq[:] would.

What’s a negative index? ¶

Python sequences are indexed with positive numbers and negative numbers. For positive numbers 0 is the first index 1 is the second index and so forth. For negative indices -1 is the last index and -2 is the penultimate (next to last) index and so forth. Think of seq[-n] as the same as seq[len(seq)-n] .

Using negative indices can be very convenient. For example S[:-1] is all of the string except for its last character, which is useful for removing the trailing newline from a string.

How do I iterate over a sequence in reverse order? ¶

Use the reversed() built-in function:

This won’t touch your original sequence, but build a new copy with reversed order to iterate over.

How do you remove duplicates from a list? ¶

See the Python Cookbook for a long discussion of many ways to do this:

https://code.activestate.com/recipes/52560/

If you don’t mind reordering the list, sort it and then scan from the end of the list, deleting duplicates as you go:

If all elements of the list may be used as set keys (i.e. they are all hashable ) this is often faster

This converts the list into a set, thereby removing duplicates, and then back into a list.

How do you remove multiple items from a list ¶

As with removing duplicates, explicitly iterating in reverse with a delete condition is one possibility. However, it is easier and faster to use slice replacement with an implicit or explicit forward iteration. Here are three variations.:

The list comprehension may be fastest.

How do you make an array in Python? ¶

Use a list:

Lists are equivalent to C or Pascal arrays in their time complexity; the primary difference is that a Python list can contain objects of many different types.

The array module also provides methods for creating arrays of fixed types with compact representations, but they are slower to index than lists. Also note that NumPy and other third party packages define array-like structures with various characteristics as well.

To get Lisp-style linked lists, you can emulate cons cells using tuples:

If mutability is desired, you could use lists instead of tuples. Here the analogue of a Lisp car is lisp_list[0] and the analogue of cdr is lisp_list[1] . Only do this if you’re sure you really need to, because it’s usually a lot slower than using Python lists.

How do I create a multidimensional list? ¶

You probably tried to make a multidimensional array like this:

This looks correct if you print it:

But when you assign a value, it shows up in multiple places:

The reason is that replicating a list with * doesn’t create copies, it only creates references to the existing objects. The *3 creates a list containing 3 references to the same list of length two. Changes to one row will show in all rows, which is almost certainly not what you want.

The suggested approach is to create a list of the desired length first and then fill in each element with a newly created list:

This generates a list containing 3 different lists of length two. You can also use a list comprehension:

Or, you can use an extension that provides a matrix datatype; NumPy is the best known.

How do I apply a method or function to a sequence of objects? ¶

To call a method or function and accumulate the return values is a list, a list comprehension is an elegant solution:

To just run the method or function without saving the return values, a plain for loop will suffice:

Why does a_tuple[i] += [‘item’] raise an exception when the addition works? ¶

This is because of a combination of the fact that augmented assignment operators are assignment operators, and the difference between mutable and immutable objects in Python.

This discussion applies in general when augmented assignment operators are applied to elements of a tuple that point to mutable objects, but we’ll use a list and += as our exemplar.

If you wrote:

The reason for the exception should be immediately clear: 1 is added to the object a_tuple[0] points to ( 1 ), producing the result object, 2 , but when we attempt to assign the result of the computation, 2 , to element 0 of the tuple, we get an error because we can’t change what an element of a tuple points to.

Under the covers, what this augmented assignment statement is doing is approximately this:

It is the assignment part of the operation that produces the error, since a tuple is immutable.

When you write something like:

The exception is a bit more surprising, and even more surprising is the fact that even though there was an error, the append worked:

To see why this happens, you need to know that (a) if an object implements an __iadd__() magic method, it gets called when the += augmented assignment is executed, and its return value is what gets used in the assignment statement; and (b) for lists, __iadd__() is equivalent to calling extend() on the list and returning the list. That’s why we say that for lists, += is a “shorthand” for list.extend() :

This is equivalent to:

The object pointed to by a_list has been mutated, and the pointer to the mutated object is assigned back to a_list . The end result of the assignment is a no-op, since it is a pointer to the same object that a_list was previously pointing to, but the assignment still happens.

Thus, in our tuple example what is happening is equivalent to:

The __iadd__() succeeds, and thus the list is extended, but even though result points to the same object that a_tuple[0] already points to, that final assignment still results in an error, because tuples are immutable.

I want to do a complicated sort: can you do a Schwartzian Transform in Python? ¶

The technique, attributed to Randal Schwartz of the Perl community, sorts the elements of a list by a metric which maps each element to its “sort value”. In Python, use the key argument for the list.sort() method:

How can I sort one list by values from another list? ¶

Merge them into an iterator of tuples, sort the resulting list, and then pick out the element you want.

What is a class? ¶

A class is the particular object type created by executing a class statement. Class objects are used as templates to create instance objects, which embody both the data (attributes) and code (methods) specific to a datatype.

A class can be based on one or more other classes, called its base class(es). It then inherits the attributes and methods of its base classes. This allows an object model to be successively refined by inheritance. You might have a generic Mailbox class that provides basic accessor methods for a mailbox, and subclasses such as MboxMailbox , MaildirMailbox , OutlookMailbox that handle various specific mailbox formats.

What is a method? ¶

A method is a function on some object x that you normally call as x.name(arguments...) . Methods are defined as functions inside the class definition:

What is self? ¶

Self is merely a conventional name for the first argument of a method. A method defined as meth(self, a, b, c) should be called as x.meth(a, b, c) for some instance x of the class in which the definition occurs; the called method will think it is called as meth(x, a, b, c) .

See also Why must ‘self’ be used explicitly in method definitions and calls? .

How do I check if an object is an instance of a given class or of a subclass of it? ¶

Use the built-in function isinstance(obj, cls) . You can check if an object is an instance of any of a number of classes by providing a tuple instead of a single class, e.g. isinstance(obj, (class1, class2, ...)) , and can also check whether an object is one of Python’s built-in types, e.g. isinstance(obj, str) or isinstance(obj, (int, float, complex)) .

Note that isinstance() also checks for virtual inheritance from an abstract base class . So, the test will return True for a registered class even if hasn’t directly or indirectly inherited from it. To test for “true inheritance”, scan the MRO of the class:

Note that most programs do not use isinstance() on user-defined classes very often. If you are developing the classes yourself, a more proper object-oriented style is to define methods on the classes that encapsulate a particular behaviour, instead of checking the object’s class and doing a different thing based on what class it is. For example, if you have a function that does something:

A better approach is to define a search() method on all the classes and just call it:

What is delegation? ¶

Delegation is an object oriented technique (also called a design pattern). Let’s say you have an object x and want to change the behaviour of just one of its methods. You can create a new class that provides a new implementation of the method you’re interested in changing and delegates all other methods to the corresponding method of x .

Python programmers can easily implement delegation. For example, the following class implements a class that behaves like a file but converts all written data to uppercase:

Here the UpperOut class redefines the write() method to convert the argument string to uppercase before calling the underlying self._outfile.write() method. All other methods are delegated to the underlying self._outfile object. The delegation is accomplished via the __getattr__() method; consult the language reference for more information about controlling attribute access.

Note that for more general cases delegation can get trickier. When attributes must be set as well as retrieved, the class must define a __setattr__() method too, and it must do so carefully. The basic implementation of __setattr__() is roughly equivalent to the following:

Most __setattr__() implementations must modify self.__dict__ to store local state for self without causing an infinite recursion.

How do I call a method defined in a base class from a derived class that extends it? ¶

Use the built-in super() function:

In the example, super() will automatically determine the instance from which it was called (the self value), look up the method resolution order (MRO) with type(self).__mro__ , and return the next in line after Derived in the MRO: Base .

How can I organize my code to make it easier to change the base class? ¶

You could assign the base class to an alias and derive from the alias. Then all you have to change is the value assigned to the alias. Incidentally, this trick is also handy if you want to decide dynamically (e.g. depending on availability of resources) which base class to use. Example:

How do I create static class data and static class methods? ¶

Both static data and static methods (in the sense of C++ or Java) are supported in Python.

For static data, simply define a class attribute. To assign a new value to the attribute, you have to explicitly use the class name in the assignment:

c.count also refers to C.count for any c such that isinstance(c, C) holds, unless overridden by c itself or by some class on the base-class search path from c.__class__ back to C .

Caution: within a method of C, an assignment like self.count = 42 creates a new and unrelated instance named “count” in self ’s own dict. Rebinding of a class-static data name must always specify the class whether inside a method or not:

Static methods are possible:

However, a far more straightforward way to get the effect of a static method is via a simple module-level function:

If your code is structured so as to define one class (or tightly related class hierarchy) per module, this supplies the desired encapsulation.

How can I overload constructors (or methods) in Python? ¶

This answer actually applies to all methods, but the question usually comes up first in the context of constructors.

In C++ you’d write

In Python you have to write a single constructor that catches all cases using default arguments. For example:

This is not entirely equivalent, but close enough in practice.

You could also try a variable-length argument list, e.g.

The same approach works for all method definitions.

I try to use __spam and I get an error about _SomeClassName__spam. ¶

Variable names with double leading underscores are “mangled” to provide a simple but effective way to define class private variables. Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam , where classname is the current class name with any leading underscores stripped.

This doesn’t guarantee privacy: an outside user can still deliberately access the “_classname__spam” attribute, and private values are visible in the object’s __dict__ . Many Python programmers never bother to use private variable names at all.

My class defines __del__ but it is not called when I delete the object. ¶

There are several possible reasons for this.

The del statement does not necessarily call __del__() – it simply decrements the object’s reference count, and if this reaches zero __del__() is called.

If your data structures contain circular links (e.g. a tree where each child has a parent reference and each parent has a list of children) the reference counts will never go back to zero. Once in a while Python runs an algorithm to detect such cycles, but the garbage collector might run some time after the last reference to your data structure vanishes, so your __del__() method may be called at an inconvenient and random time. This is inconvenient if you’re trying to reproduce a problem. Worse, the order in which object’s __del__() methods are executed is arbitrary. You can run gc.collect() to force a collection, but there are pathological cases where objects will never be collected.

Despite the cycle collector, it’s still a good idea to define an explicit close() method on objects to be called whenever you’re done with them. The close() method can then remove attributes that refer to subobjects. Don’t call __del__() directly – __del__() should call close() and close() should make sure that it can be called more than once for the same object.

Another way to avoid cyclical references is to use the weakref module, which allows you to point to objects without incrementing their reference count. Tree data structures, for instance, should use weak references for their parent and sibling references (if they need them!).

Finally, if your __del__() method raises an exception, a warning message is printed to sys.stderr .

How do I get a list of all instances of a given class? ¶

Python does not keep track of all instances of a class (or of a built-in type). You can program the class’s constructor to keep track of all instances by keeping a list of weak references to each instance.

Why does the result of id() appear to be not unique? ¶

The id() builtin returns an integer that is guaranteed to be unique during the lifetime of the object. Since in CPython, this is the object’s memory address, it happens frequently that after an object is deleted from memory, the next freshly created object is allocated at the same position in memory. This is illustrated by this example:

The two ids belong to different integer objects that are created before, and deleted immediately after execution of the id() call. To be sure that objects whose id you want to examine are still alive, create another reference to the object:

When can I rely on identity tests with the is operator? ¶

The is operator tests for object identity. The test a is b is equivalent to id(a) == id(b) .

The most important property of an identity test is that an object is always identical to itself, a is a always returns True . Identity tests are usually faster than equality tests. And unlike equality tests, identity tests are guaranteed to return a boolean True or False .

However, identity tests can only be substituted for equality tests when object identity is assured. Generally, there are three circumstances where identity is guaranteed:

1) Assignments create new names but do not change object identity. After the assignment new = old , it is guaranteed that new is old .

2) Putting an object in a container that stores object references does not change object identity. After the list assignment s[0] = x , it is guaranteed that s[0] is x .

3) If an object is a singleton, it means that only one instance of that object can exist. After the assignments a = None and b = None , it is guaranteed that a is b because None is a singleton.

In most other circumstances, identity tests are inadvisable and equality tests are preferred. In particular, identity tests should not be used to check constants such as int and str which aren’t guaranteed to be singletons:

Likewise, new instances of mutable containers are never identical:

In the standard library code, you will see several common patterns for correctly using identity tests:

1) As recommended by PEP 8 , an identity test is the preferred way to check for None . This reads like plain English in code and avoids confusion with other objects that may have boolean values that evaluate to false.

2) Detecting optional arguments can be tricky when None is a valid input value. In those situations, you can create a singleton sentinel object guaranteed to be distinct from other objects. For example, here is how to implement a method that behaves like dict.pop() :

3) Container implementations sometimes need to augment equality tests with identity tests. This prevents the code from being confused by objects such as float('NaN') that are not equal to themselves.

For example, here is the implementation of collections.abc.Sequence.__contains__() :

How can a subclass control what data is stored in an immutable instance? ¶

When subclassing an immutable type, override the __new__() method instead of the __init__() method. The latter only runs after an instance is created, which is too late to alter data in an immutable instance.

All of these immutable classes have a different signature than their parent class:

The classes can be used like this:

How do I cache method calls? ¶

The two principal tools for caching methods are functools.cached_property() and functools.lru_cache() . The former stores results at the instance level and the latter at the class level.

The cached_property approach only works with methods that do not take any arguments. It does not create a reference to the instance. The cached method result will be kept only as long as the instance is alive.

The advantage is that when an instance is no longer used, the cached method result will be released right away. The disadvantage is that if instances accumulate, so too will the accumulated method results. They can grow without bound.

The lru_cache approach works with methods that have hashable arguments. It creates a reference to the instance unless special efforts are made to pass in weak references.

The advantage of the least recently used algorithm is that the cache is bounded by the specified maxsize . The disadvantage is that instances are kept alive until they age out of the cache or until the cache is cleared.

This example shows the various techniques:

The above example assumes that the station_id never changes. If the relevant instance attributes are mutable, the cached_property approach can’t be made to work because it cannot detect changes to the attributes.

To make the lru_cache approach work when the station_id is mutable, the class needs to define the __eq__() and __hash__() methods so that the cache can detect relevant attribute updates:

How do I create a .pyc file? ¶

When a module is imported for the first time (or when the source file has changed since the current compiled file was created) a .pyc file containing the compiled code should be created in a __pycache__ subdirectory of the directory containing the .py file. The .pyc file will have a filename that starts with the same name as the .py file, and ends with .pyc , with a middle component that depends on the particular python binary that created it. (See PEP 3147 for details.)

One reason that a .pyc file may not be created is a permissions problem with the directory containing the source file, meaning that the __pycache__ subdirectory cannot be created. This can happen, for example, if you develop as one user but run as another, such as if you are testing with a web server.

Unless the PYTHONDONTWRITEBYTECODE environment variable is set, creation of a .pyc file is automatic if you’re importing a module and Python has the ability (permissions, free space, etc…) to create a __pycache__ subdirectory and write the compiled module to that subdirectory.

Running Python on a top level script is not considered an import and no .pyc will be created. For example, if you have a top-level module foo.py that imports another module xyz.py , when you run foo (by typing python foo.py as a shell command), a .pyc will be created for xyz because xyz is imported, but no .pyc file will be created for foo since foo.py isn’t being imported.

If you need to create a .pyc file for foo – that is, to create a .pyc file for a module that is not imported – you can, using the py_compile and compileall modules.

The py_compile module can manually compile any module. One way is to use the compile() function in that module interactively:

This will write the .pyc to a __pycache__ subdirectory in the same location as foo.py (or you can override that with the optional parameter cfile ).

You can also automatically compile all files in a directory or directories using the compileall module. You can do it from the shell prompt by running compileall.py and providing the path of a directory containing Python files to compile:

How do I find the current module name? ¶

A module can find out its own module name by looking at the predefined global variable __name__ . If this has the value '__main__' , the program is running as a script. Many modules that are usually used by importing them also provide a command-line interface or a self-test, and only execute this code after checking __name__ :

How can I have modules that mutually import each other? ¶

Suppose you have the following modules:

The problem is that the interpreter will perform the following steps:

main imports foo

Empty globals for foo are created

foo is compiled and starts executing

foo imports bar

Empty globals for bar are created

bar is compiled and starts executing

bar imports foo (which is a no-op since there already is a module named foo )

The import mechanism tries to read foo_var from foo globals, to set bar.foo_var = foo.foo_var

The last step fails, because Python isn’t done with interpreting foo yet and the global symbol dictionary for foo is still empty.

The same thing happens when you use import foo , and then try to access foo.foo_var in global code.

There are (at least) three possible workarounds for this problem.

Guido van Rossum recommends avoiding all uses of from <module> import ... , and placing all code inside functions. Initializations of global variables and class variables should use constants or built-in functions only. This means everything from an imported module is referenced as <module>.<name> .

Jim Roskind suggests performing steps in the following order in each module:

exports (globals, functions, and classes that don’t need imported base classes)

import statements

active code (including globals that are initialized from imported values).

Van Rossum doesn’t like this approach much because the imports appear in a strange place, but it does work.

Matthias Urlichs recommends restructuring your code so that the recursive import is not necessary in the first place.

These solutions are not mutually exclusive.

__import__(‘x.y.z’) returns <module ‘x’>; how do I get z? ¶

Consider using the convenience function import_module() from importlib instead:

When I edit an imported module and reimport it, the changes don’t show up. Why does this happen? ¶

For reasons of efficiency as well as consistency, Python only reads the module file on the first time a module is imported. If it didn’t, in a program consisting of many modules where each one imports the same basic module, the basic module would be parsed and re-parsed many times. To force re-reading of a changed module, do this:

Warning: this technique is not 100% fool-proof. In particular, modules containing statements like

will continue to work with the old version of the imported objects. If the module contains class definitions, existing class instances will not be updated to use the new class definition. This can result in the following paradoxical behaviour:

The nature of the problem is made clear if you print out the “identity” of the class objects:

Table of Contents

  • General Questions
  • Core Language
  • Numbers and strings
  • Performance
  • Sequences (Tuples/Lists)

Previous topic

General Python FAQ

Design and History FAQ

  • Report a Bug
  • Show Source

Submission Instruction

  • Lab 7 - Video Processing
  • Lab 8 (extra credit) - Digit Recognition
  • Assigned Project Lab
  • Submission Instructions

Lab 6 - Image Processing

In this lab, you will learn how to process an image using histogram equalization and 2-D convolution. An example of the final solution can be found here .

Python project source code and test image

Android project source code

As in Lab 6, your task for the Python portion of this lab will be to prototype your Android system. You will be given an target test image and you will apply histogram equalization to it or you will perform 2-D convolution using a given kernel.

Part 1 - Histogram Equalization

python assignment by value or reference

We will be applying histogram equalization to eco.tif . Recall from the prelab, histogram equalization is a technique for adjusting image intensities to enhance contrast. It is done by the following procedure.

Compute Histogram of Image

Histogram is the statistic representation of an image. It records the number of times that a certain intensity value appears in the entire image. A simple example is shown below:

python assignment by value or reference

Your histogram can be represented simply as an array. What size will the array need to be? What type?

Compute CDF of Histogram

Next, you must compute the cumulative distribution function (CDF) of your histogram. CDF(x) is the number of pixels whose value is less or equal than x. After computing CDF, you must make sure to normalize your CDF so that the range is 0 to 65535 (uint16). The formula for normalization is:

where cdfmin is the minimum non-zero value of the cumulative distribution function, M x N gives the image's number of pixels (where M is width and N the height) and L is the number of gray levels used (65536 for uint16 ). reference

Apply Histogram Equalization

Substitute each of the pixel value of your original picture to the new normalized CDF value you just computed.

Assignment 1

Implement the Python system ( histeq() in lab6Histeq.py ) described above, and show the result of the image after applying Histogram Equalization. These plot will count for 1 demo points.

Part 2 - 2-D Convolution

The idea of 2-D Convolution is relatively straight forward. You'll apply the filter to every pixel in the image and substitute the pixel value with the convolution result. Here, we assume that the picture is padded with zeros around the edges, which means that when the kernel is applied to the edge pixels, we treat the values out of the image boundaries as zeros.

Same as 1-D convolution, we need to flip the entire filter before applying convolution. Make sure you remember this! However, most image filters are central symmetric, so the results will still be the same even if you forgot to deal with the filter.

If you have any questions about how to perform 2-D convolution, refer to the example here. More explanation and examples of 2-D Convolution

In python, we will apply 2-D convolution to two images, kitten.png and logo.png , both are RGB pictures, so you will have to apply 2-D convolution to all three channels. Your code should be general enough to take in images or kernels of different sizes. Pay attention to the data type, the images are typically of uint8 type, while the kernels are often of double type.

Assignment 2

Implement the Python system ( conv2() in lab6Conv2.py ) described above, and show the result of the image after applying Convolution. The entire code may take a long running time up to 80 seconds. These plot will count for 1 demo points.

Part 3 - System Requirements

The Python test code has been given to mimic the final Android system. You will get an input image eco.tif of size 512 X 672 , and you will apply histogram equalization to it. The result should be very similar to the result in prelab. Some things to think about:

  • How would you like to store your intermediate results, like histogram and CDF? What could you do to make them more efficient?
  • Why do we need to normalize CDF and how does it works?

For this lab, we will be implementing in Java, which has a very similar syntax with C++. If you've implemented your Python system without relying on many built-ins, it should translate nicely to Android. Your code will reside in java\com\ece420\lab6 .

Part 4 - Data Specification

It is worth mentioning that the frame images that you'll get from the Android camera is a little different. Instead of the commonly know RGB color space you would think of, the Android default camera images are generated in the YUV color space. More specifically, the data is in Y'UV420p(NV21) format. The YUV color space is actually more intuitive to the human interpretation of vision, for the Y channel encodes the luma component (brightness), and the U and V are the chroma (color) components. Simply speaking, the Y channel will be containing most of the information of the image, and this is why we will only manipulate this channel. More explanation of YUV specifications and why YUV is preferred over RGB can be find here and here . You don't need to understand how YUV works other than we are only manipulating the Y channel, but please pay attention to the following description.

Similar to audio processing, the Android Camera Hardware will provide you with image frames when they are ready, but the frames is given in a 1-D array as byte[] data , instead of a 2-D array.

Following the YUV format, if we have an image of M x N, the byte[] data array we get will be of length M x N x 1.5 , as shown below:

python assignment by value or reference

Since we are only manipulating the Y channel which contains intensity information, we only need to manipulate the part within range data[0 : M x N - 1] .

Part 5 - Image Coordinates

The coordinate of images are shown in the figure below. The x-axis corresponds to the row/height and the y-axis corresponds col/width. The transformation of 2-D image index to 1-D data index is simple.

python assignment by value or reference

Part 6 - Other Explanations

Our Tablet camera has a very powerful camera with a high resolution, but to cater our need for this lab, we are setting the camera to generate image frames of 640 X 480 . Also the preview image has been rotated 90 degrees to accomadate protrait mode. There is a part in the java file that change the camera settings, feel free to play with it.

Android devices tend to have limited performance when battery is low. It is because the system is designed to turn off a lot of hardware resources when the battery gets too low so that the device will last for longer. Make sure your tablet is fully charged before demo or your app might be slower than your algorithm should expect.

The kernels that we uses are of size 3 X 3 , you may assume this will not change, but it won't hurt to implement your code that could adapt kernels of any size.

The entire code works in an callback fashion as before. The camera preview is drawn to the 'surfaceView' object automatically handled by the Android Camera Class. Each time a new frame ready, the callback function is called and provided with the new frame data byte[] data . Inside, the callback function, we setup the function that will pass the input frame data to your function ( histeq() and conv2 ). Your output will be further manipulated to create the RGB or Grayscale bitmap and then drawn to another 'surfaceView' object down below.

Assignment 3

Implement the Histogram Equalization and 2-D Convolution in java\com\ece420\lab6 . The function you will need to implement are histeq() and conv2() at the end of the entire code.

The Android implementation will count for two demo points.

  • Under which scenario will the histogram equalization make a significant difference?
  • How many computations do you need to do for each frame 2-D convolution.

For Histogram Equalization on Android, you will be working with directly uint8_t (or byte ) types, the scaling is now from 0 to 255 . Also, Java byte type is signed, meaning that pixel value 255 , which is 0xFF , will be interpreted as -1 by the compiler, be careful we you use byte data directly.

Refer to the Submission Instruction page for more information.

Lab 2 will be graded as follows:

Prelab [2 points]

Lab [4 points]

Assignment 1 [1 point] Histogram equalization implementation [1 point]
2-D convolution implementation [1 point]
Assignment 3 [2 points] Histogram equalization implementation [0.5 point] 2-D convolution implementation [0.5 point] Camera not stucked or skipping too many frames [0.5 point] Filter functionality and app demo [0.5 point]

Quiz [2 points]

IMAGES

  1. Pass by reference vs value in Python

    python assignment by value or reference

  2. Python 24: Passing by Value and by Reference

    python assignment by value or reference

  3. Pass by Reference vs. Value in Python

    python assignment by value or reference

  4. pass by value and reference in python

    python assignment by value or reference

  5. Python Variable (Assign value, string Display, multiple Variables & Rules)

    python assignment by value or reference

  6. Python Call by Value or Call by Reference?

    python assignment by value or reference

VIDEO

  1. Python Variables

  2. Python Data types

  3. writing a python program to know all variables types 51dayschallenge @day4 #pythontutorial #coding

  4. Episode 03 Python Value Assign to Variable ROSHAN ROLANKA

  5. [5] Python Basics: Variables & data types

  6. Python Assignment operators Part-4 #learnpython #python3#pythonoperators#pythonforbeginners

COMMENTS

  1. Python : When is a variable passed by reference and when by value

    def compute (ob): if isinstance (ob,list): return process_list (ob) if isinstance (ob,dict): return process_dict (ob) for loc in locs: loc = compute (loc) # What to change here to make loc a reference of actual locs iteration ? locs must contain the final processed response ! I don't want to use enumerate, is it possible without it ? python

  2. Pass by reference vs value in Python

    Python's argument-passing model is neither "Pass by Value" nor "Pass by Reference" but it is "Pass by Object Reference". Depending on the type of object you pass in the function, the function behaves differently. Immutable objects show "pass by value" whereas mutable objects show "pass by reference".

  3. Pass by Reference in Python: Background and Best Practices

    By reference means that the argument you're passing to the function is a reference to a variable that already exists in memory rather than an independent copy of that variable. Since you're giving the function a reference to an existing variable, all operations performed on this reference will directly affect the variable to which it refers.

  4. Is Python call by reference or call by value

    Python utilizes a system, which is known as "Call by Object Reference" or "Call by assignment". If you pass arguments like whole numbers, strings, or tuples to a function, the passing is like a call-by-value because you can not change the value of the immutable objects being passed to the function.

  5. Python : What is it? Pass by Value or Pass by Reference? It is ...

    Python's Pass by Assignment: Python's behavior is neither purely pass-by value nor pass-by-reference. Instead, it employs a mechanism called "pass by assignment" or "call by object."...

  6. Pass-by-value, reference, and assignment

    Introduction Many traditional programming languages employ either one of two models when passing arguments to functions: some languages use the pass-by-value model; and most of the others use the pass-by-reference model. Having said that, it is important to know the model that Python uses, because that influences the way your code behaves.

  7. Python's Assignment Operator: Write Robust Assignments

    Here, variable represents a generic Python variable, while expression represents any Python object that you can provide as a concrete value—also known as a literal—or an expression that evaluates to a value. To execute an assignment statement like the above, Python runs the following steps: Evaluate the right-hand expression to produce a concrete value or object.

  8. Python Course #12: Pass by Assignment, Copy, Reference, and None

    Python Pass by Assignment When you call a pass data to a function such as print (...) there are two ways. The first option is to pass the data directly as a value: 1 2 >>> print(1, ['a', 'b']) 1 ['a', 'b'] The second option is to pass the data as a variable: 1 2 3 4 >>> i = 1 >>> l = ['a', 'b'] >>> print(i, l) 1 ['a', 'b']

  9. Pass by Assignment

    00:12 Python doesn't use either pass by value or pass by reference. It uses something called pass by assignment. Other names for this include pass by object, pass by object reference, and pass by sharing. 00:25 But I'll be using the phrase "pass by assignment." It's based on the following: First, everything in Python is an object.

  10. Is Python call-by-value or call-by-reference? Neither.

    When asked whether Python function calling model is "call-by-value" or "call-by-reference", the correct answer is: neither. Indeed, to try to shoe-horn those terms into a conversation about Python's model is misguided. "call-by-object," or "call-by-object-reference" is a more accurate way of describing it. But what does "call-by-object" even mean?

  11. Python pass by reference or value with examples

    Call by value. While calling a function, in a programming language instead of copying the values of variables, the address of the variables is used, it is known as "Call By Reference.". While calling a function, when we pass values by copying variables, it is known as "Call By Values.". In this method, a variable itself is passed.

  12. arrays

    How can I assign by value in python Asked Viewed 3 I understand that, due to the way Python works x = []; y = x; x.append (1); y will print [1]. However, the reverse, say, z = [1,2] temp = z temp [1] = 3 z,temp will print ( [1,3], [1,3]).

  13. Variables in Python

    Assignment is done with a single equals sign ( = ): Python >>> n = 300 This is read or interpreted as " n is assigned the value 300 ." Once this is done, n can be used in a statement or expression, and its value will be substituted: Python >>> print(n) 300

  14. All you need to know on by reference vs by value

    As you saw Ruby only uses pass by reference value while JavaScript uses a mixed strategy. Still, the behavior is the same for almost all the data types due to the different implementation of the data structures. Most of the mainstream languages are either copied and passed by value or copied and passed by reference value. For the last time ...

  15. how to assign variable by reference in python?

    Is there a way to have a python variable assigned by reference? So, that in the below example, it would actually change o.a to 2? class myClass (): def __init__ (self): self.a = 1 def __str__ (self): _s = '' for att in vars (self): _s += '%s, %s' % (att, getattr (self,att)) return _s o = myClass () x = o.a x = 2 print o python Share

  16. Programming FAQ

    Remember that arguments are passed by assignment in Python. Since assignment just creates references to objects, there's no alias between an argument name in the caller and callee, and so no call-by-reference per se. ... and its return value is what gets used in the assignment statement; and (b) for lists, __iadd__() is equivalent to calling ...

  17. Pass by Reference in Python: Best Practices (Summary)

    Discussion (1) Python works differently from languages that support passing arguments by reference or by value. Function arguments become local variables assigned to each value that was passed to the function. But this doesn't prevent you from achieving the same results you'd expect when passing arguments by reference in other languages.

  18. Lab 6

    Substitute each of the pixel value of your original picture to the new normalized CDF value you just computed. Assignment 1. Implement the Python system ( histeq () in lab6Histeq.py) described above, and show the result of the image after applying Histogram Equalization. These plot will count for 1 demo points.

  19. python

    2,024 3 21 33 Possible duplicate of How do I pass a variable by reference? - Thierry Lathuille Feb 19, 2018 at 18:27 5 This is the way Python assignment always works. A = B merely means "the object referred to by the name A is now referred to by the name B as well". Again, this is easy to remember, it always works this way.

  20. Object Passing in Python

    In this lesson, you'll learn that Python doesn't pass objects by value or reference. Python passes objects by assignment instead. In the examples, you'll see how this differs when the object is mutable or immutable. In previous videos, you learned about pass-by-reference and pass-by-value. To review, if you pass an object by value, you ...

  21. Arrays in Python are assigned by value or by reference?

    Arrays in Python are assigned by value or by reference? - Stack Overflow Arrays in Python are assigned by value or by reference? Ask Question Asked 7 years ago Modified 7 years ago Viewed 11k times 3 I am wondering why when I delete the original array it affects the copied array: arr = [1,2,3] arr1 = arr del arr [:] print (arr1) #this prints []

  22. Assignment Expressions

    It's called an assignment expression, and it allows you to save the return value of a function to a variable while at the same time…