1 Guktilar

Restructuring Assignment Python Found

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.


var a, b, rest; [a, b] = [10, 20]; console.log(a); // 10 console.log(b); // 20 [a, b, ...rest] = [10, 20, 30, 40, 50]; console.log(a); // 10 console.log(b); // 20 console.log(rest); // [30, 40, 50] ({ a, b } = { a: 10, b: 20 }); console.log(a); // 10 console.log(b); // 20 // Stage 3 proposal ({a, b, ...rest} = {a: 10, b: 20, c: 30, d: 40}); console.log(a); // 10 console.log(b); // 20 console.log(rest); //{c: 30, d: 40}


The object and array literal expressions provide an easy way to create ad hoc packages of data.

var x = [1, 2, 3, 4, 5];

The destructuring assignment uses similar syntax, but on the left-hand side of the assignment to define what values to unpack from the sourced variable.

var x = [1, 2, 3, 4, 5]; var [y, z] = x; console.log(y); // 1 console.log(z); // 2

This capability is similar to features present in languages such as Perl and Python.

Array destructuring

Basic variable assignment

var foo = ['one', 'two', 'three']; var [one, two, three] = foo; console.log(one); // "one" console.log(two); // "two" console.log(three); // "three"

Assignment separate from declaration

A variable can be assigned its value via destructuring separate from the variable's declaration.

var a, b; [a, b] = [1, 2]; console.log(a); // 1 console.log(b); // 2

Default values

A variable can be assigned a default, in the case that the value unpacked from the array is .

var a, b; [a=5, b=7] = [1]; console.log(a); // 1 console.log(b); // 7

Swapping variables

Two variables values can be swapped in one destructuring expression.

Without destructuring assignment, swapping two values requires a temporary variable (or, in some low-level languages, the XOR-swap trick).

var a = 1; var b = 3; [a, b] = [b, a]; console.log(a); // 3 console.log(b); // 1

Parsing an array returned from a function

It's always been possible to return an array from a function. Destructuring can make working with an array return value more concise.

In this example, returns the values as its output, which can be parsed in a single line with destructuring.

function f() { return [1, 2]; } var a, b; [a, b] = f(); console.log(a); // 1 console.log(b); // 2

Ignoring some returned values

You can ignore return values that you're not interested in:

function f() { return [1, 2, 3]; } var [a, , b] = f(); console.log(a); // 1 console.log(b); // 3

You can also ignore all returned values:

[,,] = f();

Assigning the rest of an array to a variable

When destructuring an array, you can unpack and assign the remaining part of it to a variable using the rest pattern:

var [a, ...b] = [1, 2, 3]; console.log(a); // 1 console.log(b); // [2, 3]

Note that a will be thrown if a trailing comma is used on the left-hand side with a rest element:

var [a, ...b,] = [1, 2, 3]; // SyntaxError: rest element may not have a trailing comma

Unpacking values from a regular expression match

When the regular expression method finds a match, it returns an array containing first the entire matched portion of the string and then the portions of the string that matched each parenthesized group in the regular expression. Destructuring assignment allows you to unpack the parts out of this array easily, ignoring the full match if it is not needed.

function parseProtocol(url) { var parsedURL = /^(\w+)\:\/\/([^\/]+)\/(.*)$/.exec(url); if (!parsedURL) { return false; } console.log(parsedURL); // ["https://developer.mozilla.org/en-US/Web/JavaScript", "https", "developer.mozilla.org", "en-US/Web/JavaScript"] var [, protocol, fullhost, fullpath] = parsedURL; return protocol; } console.log(parseProtocol('https://developer.mozilla.org/en-US/Web/JavaScript')); // "https"

Object destructuring

Basic assignment

var o = {p: 42, q: true}; var {p, q} = o; console.log(p); // 42 console.log(q); // true

Assignment without declaration

A variable can be assigned its value with destructuring separate from its declaration.

var a, b; ({a, b} = {a: 1, b: 2});

The round braces  around the assignment statement is required syntax when using object literal destructuring assignment without a declaration.

is not valid stand-alone syntax, as the on the left-hand side is considered a block and not an object literal.

However, is valid, as is

Assigning to new variable names

A property can be unpacked from an object and assigned to a variable with a different name than the object property.

var o = {p: 42, q: true}; var {p: foo, q: bar} = o; console.log(foo); // 42 console.log(bar); // true

Default values

A variable can be assigned a default, in the case that the value unpacked from the object is .

var {a = 10, b = 5} = {a: 3}; console.log(a); // 3 console.log(b); // 5

Assigning to new variables names and providing default values

A property can be both 1) unpacked from an object and assigned to a variable with a different name and 2) assigned a default value in case the unpacked value is .

var {a:aa = 10, b:bb = 5} = {a: 3}; console.log(aa); // 3 console.log(bb); // 5

Setting a function parameter's default value

ES5 version

function drawES5Chart(options) { options = options === undefined ? {} : options; var size = options.size === undefined ? 'big' : options.size; var cords = options.cords === undefined ? {x: 0, y: 0} : options.cords; var radius = options.radius === undefined ? 25 : options.radius; console.log(size, cords, radius); // now finally do some chart drawing } drawES5Chart({ cords: {x: 18, y: 30}, radius: 30 });

ES2015 version

function drawES2015Chart({size = 'big', cords = {x: 0, y: 0}, radius = 25} = {}) { console.log(size, cords, radius); // do some chart drawing } drawES2015Chart({ cords: {x: 18, y: 30}, radius: 30 });

In the function signature for above, the destructured left-hand side is assigned to an empty object literal on the right-hand side: . You could have also written the function without the right-hand side assignment. However, if you leave out the right-hand side assignment, the function will look for at least one argument to be supplied when invoked, whereas in its current form, you can simply call without supplying any parameters. The current design is useful if you want to be able to call the function without supplying any parameters, the other can be useful when you want to ensure an object is passed to the function.

Nested object and array destructuring

var metadata = { title: 'Scratchpad', translations: [ { locale: 'de', localization_tags: [], last_edit: '2014-04-14T08:43:37', url: '/de/docs/Tools/Scratchpad', title: 'JavaScript-Umgebung' } ], url: '/en-US/docs/Tools/Scratchpad' }; var {title: englishTitle, translations: [{title: localeTitle}]} = metadata; console.log(englishTitle); // "Scratchpad" console.log(localeTitle); // "JavaScript-Umgebung"

For of iteration and destructuring

var people = [ { name: 'Mike Smith', family: { mother: 'Jane Smith', father: 'Harry Smith', sister: 'Samantha Smith' }, age: 35 }, { name: 'Tom Jones', family: { mother: 'Norah Jones', father: 'Richard Jones', brother: 'Howard Jones' }, age: 25 } ]; for (var {name: n, family: {father: f}} of people) { console.log('Name: ' + n + ', Father: ' + f); } // "Name: Mike Smith, Father: Harry Smith" // "Name: Tom Jones, Father: Richard Jones"

Unpacking fields from objects passed as function parameter

function userId({id}) { return id; } function whois({displayName, fullName: {firstName: name}}) { console.log(displayName + ' is ' + name); } var user = { id: 42, displayName: 'jdoe', fullName: { firstName: 'John', lastName: 'Doe' } }; console.log('userId: ' + userId(user)); // "userId: 42" whois(user); // "jdoe is John"

This unpacks the , and from the user object and prints them.

Computed object property names and destructuring

Computed property names, like on object literals, can be used with destructuring.

let key = 'z'; let {[key]: foo} = {z: 'bar'}; console.log(foo); // "bar"

Rest in Object Destructuring

The Rest/Spread Properties for ECMAScript proposal (stage 3) adds the rest syntax to destructuring. Rest properties collect the remaining own enumerable property keys that are not already picked off by the destructuring pattern.

let {a, b, ...rest} = {a: 10, b: 20, c: 30, d: 40} a; // 10 b; // 20 rest; // { c: 30, d: 40 }

Invalid JavaScript identifier as a property name

Destructuring can be used with property names that are not valid JavaScript identifiers by providing an alternative identifer that is valid.

const foo = { 'fizz-buzz': true }; const { 'fizz-buzz': fizzBuzz } = foo; console.log(fizzBuzz); // "true"


Browser compatibility

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support4914411 No Yes8
Computed property names491434 No Yes No
Rest in arrays4914234 No Yes No
Rest in objects60 No55 No Yes No
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support494914411 Yes8 ?
Computed property names49491434 Yes No ?
Rest in arrays494914234 Yes No ?
Rest in objects6060 No55 Yes No ?

1. Firefox provided a non-standard destructuring implementation from Firefox 2 to 40.

2. From version 14: this feature is behind the preference.

ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung InternetNode.js
Basic supportFull support 49Full support 14Full support 41


Full support 41


Notes Firefox provided a non-standard destructuring implementation from Firefox 2 to 40.
No support NoFull support YesFull support 8Full support 49Full support 49Full support 14Full support 41


Full support 41


Notes Firefox provided a non-standard destructuring implementation from Firefox 2 to 40.
Full support YesFull support 8 ? Full support Yes
Computed property namesFull support 49Full support 14Full support 34No support NoFull support YesNo support NoFull support 49Full support 49Full support 14Full support 34Full support YesNo support No ? Full support Yes
Rest in arraysFull support 49Full support 14


Full support 14


Disabled From version 14: this feature is behind the preference.
Full support 34No support NoFull support YesNo support NoFull support 49Full support 49Full support 14


Full support 14


Disabled From version 14: this feature is behind the preference.
Full support 34Full support YesNo support No ? Full support Yes
Rest in objects


Full support 60No support NoFull support 55No support NoFull support YesNo support NoFull support 60Full support 60No support NoFull support 55Full support YesNo support No ? Full support Yes


Full support
Full support
No support
No support
Compatibility unknown
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

See also

General Questions¶

Is there a source code level debugger with breakpoints, single-stepping, etc.?¶


The pdb module is a simple but adequate console-mode debugger for Python. It is part of the standard Python library, and is . 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/idle), 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 the Python for Windows Extensions project and as a part of the ActivePython distribution (see https://www.activestate.com/activepython).

Boa Constructor is an IDE and GUI builder that uses wxWidgets. It offers visual frame creation and manipulation, an object inspector, many views on the source like object browsers, inheritance hierarchies, doc string generated html documentation, an advanced debugger, integrated help, and Zope support.

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

Pydb is a version of the standard Python debugger pdb, modified for use with DDD (Data Display Debugger), a popular graphical debugger front end. Pydb can be found at http://bashdb.sourceforge.net/pydb/ and DDD can be found at https://www.gnu.org/software/ddd.

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

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 . It converts Python byte code to C arrays; 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.

Obviously, freeze requires a C compiler. There are several other utilities which don’t. One is Thomas Heller’s py2exe (Windows only) at


Another tool is Anthony Tuininga’s cx_Freeze.

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.

This code:

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 , the compiler recognizes it as a local variable. Consequently when the earlier 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 keyword:

>>> x=10>>> defbar():... print(x)>>> bar()10
>>> x=10>>> deffoo():... print(x)... x+=1
>>> foo()Traceback (most recent call last):...UnboundLocalError: local variable 'x' referenced before assignment
>>> x=10>>> deffoobar():... globalx... print(x)... x+=1>>> foobar()10
>>> deffoo():... x=10... defbar():... nonlocalx... print(x)... x+=1... bar()... print(x)>>> foo()1011

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 for assigned variables provides a bar against unintended side-effects. On the other hand, if was required for all global references, you’d be using 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 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 . You might expect that, when called, they would return, respectively, , , , , and . However, when you actually try you will see that they all return :

This happens because 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 is , so all the functions now return , i.e. . You can also verify this by changing the value of 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 :

Here, creates a new variable local to the lambda and computed when the lambda is defined so that it has the same value that had at that point in the loop. This means that the value of will be in the first lambda, in the second, 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.

>>> squares=[]>>> forxinrange(5):... squares.append(lambda:x**2)
>>> squares[2]()16>>> squares[4]()16
>>> x=8>>> squares[2]()64
>>> squares=[]>>> forxinrange(5):... squares.append(lambdan=x:n**2)
>>> squares[2]()4>>> squares[4]()16

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.

x=0# Default value of the 'x' configuration setting

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

In general, don’t use . 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:

  1. standard library modules – e.g. , , ,
  2. third-party library modules (anything installed in Python’s site-packages directory) – e.g. mx.DateTime, ZODB, PIL.Image, etc.
  3. 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 .

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, contains a single item. The second time, contains two items because when begins executing, 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 , 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 as the default value and inside the function, check if the parameter is 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.

deffoo(mydict={}):# Danger: shared reference to one dict for all calls...computesomething...mydict[key]=valuereturnmydict
deffoo(mydict=None):ifmydictisNone:mydict={}# create a new dict for local namespace
# Callers will never provide a third parameter for this function.defexpensive(arg1,arg2,_cache={}):if(arg1,arg2)in_cache:return_cache[(arg1,arg2)]# Calculate the valueresult=...expensivecomputation..._cache[(arg1,arg2)]=result# Store result in the cachereturnresult

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 types of arguments a function can accept. For example, given the function definition:

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

the values , , and 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 changed too.

There are two factors that produce this result:

  1. Variables are simply names that refer to objects. Doing doesn’t create a copy of the list – it creates a new variable that refers to the same object refers to. This means that there is only one object (the list), and both and refer to it.
  2. Lists are mutable, which means that you can change their content.

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

If we instead assign an immutable object to :

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

Some operations (for example and ) mutate the object, whereas superficially similar operations (for example and ) create a new object. In general in Python (and in all cases in the standard library) a method that mutates an object will return to help avoid getting the two types of operations confused. So if you mistakenly write thinking it will give you a sorted copy of , you’ll instead end up with , 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 ( is equivalent to and mutates , whereas and create new objects).

In other words:

  • If we have a mutable object (, , , 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 (, , , 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 operator, or the built-in function .

>>> x=[]>>> y=x>>> y.append(10)>>> y[10]>>> x[10]
>>> x=5# ints are immutable>>> y=x>>> x=x+1# 5 can't be mutated, we are creating a new object here>>> x6>>> y5

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.

  1. By returning a tuple of the results:

    This is almost always the clearest solution.

    deffunc2(a,b):a='new-value'# a and b are local namesb=b+1# assigned to new objectsreturna,b# return new valuesx,y='old-value',99x,y=func2(x,y)print(x,y)# output: new-value 100
  2. By using global variables. This isn’t thread-safe, and is not recommended.

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

    deffunc1(a):a[0]='new-value'# 'a' references a mutable lista[1]=a[1]+1# changes a shared objectargs=['old-value',99]func1(args)print(args[0],args[1])# output: new-value 100
  4. By passing in a dictionary that gets mutated:

    deffunc3(args):args['a']='new-value'# args is a mutable dictionaryargs['b']=args['b']+1# change it in-placeargs={'a':'old-value','b':99}func3(args)print(args['a'],args['b'])
  5. Or bundle up values in a class instance:

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

    classcallByRef:def__init__(self,**args):for(key,value)inargs.items():setattr(self,key,value)deffunc4(args):args.a='new-value'# args is a mutable callByRefargs.b=args.b+1# change object in-placeargs=callByRef(a='old-value',b=99)func4(args)print(args.a,args.b)

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 which returns a function that computes the value . Using nested scopes:

Or using a callable object:

In both cases,

gives a callable object where .

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 , and act like functions which share the same counting variable.

classexponential(linear):# __init__ inheriteddef__call__(self,x):returnself.a*(x**self.b)

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 and 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!

>>> classA:... pass...>>> B=A>>> a=B()>>> b=a>>> print(b)<__main__.A object at 0x16D07CC>>>> print(a)<__main__.A object at 0x16D07CC>

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.

>>> "a"in"b","a"(False, 'a')

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 form.


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

Yes. Usually this is done by nesting within . See the following three examples, due to Ulf Bartelt:

Don’t try this at home, kids!

fromfunctoolsimportreduce# Primes < 1000print(list(filter(None,map(lambday:y*reduce(lambdax,y:x*y!=0,map(lambdax,y=y:y%x,range(2,int(pow(y,0.5)+1))),1),range(2,1000)))))# First 10 Fibonacci numbersprint(list(map(lambdax,f=lambdax,f:(f(x-1,f)+f(x-2,f))ifx>1else1:f(x,f),range(10))))# Mandelbrot setprint((lambdaRu,Ro,Iu,Io,IM,Sx,Sy:reduce(lambdax,y:x+y,map(lambday,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambdayc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,Sx=Sx,Sy=Sy:reduce(lambdax,y:x+y,map(lambdax,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,i=i,Sx=Sx,F=lambdaxc,yc,x,y,k,f=lambdaxc,yc,x,y,k,f:(k<=0)or(x*x+y*y>=4.0)or1+f(xc,yc,x*x-y*y+xc,2.0*x*y+yc,k-1,f):f(xc,yc,x,y,k,f):chr(64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy))))(-2.1,0.7,-1.2,1.2,30,80,24))# \___ ___/ \___ ___/ | | |__ lines on screen# V V | |______ columns on screen# | | |__________ maximum of "iterations"# | |_________________ range on y axis# |____________________________ range on x axis

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:

>>> a=0o10>>> a8
>>> a=0xa5>>> a165>>> b=0XB2>>> b178

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

It’s primarily driven by the desire that have the same sign as . 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 need to make have the same sign as .

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


How do I convert a string to a number?¶

For integers, use the built-in type constructor, e.g. . Similarly, converts to floating-point, e.g. .

By default, these interpret the number as decimal, so that and raises . takes the base to convert from as a second optional argument, so . 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 if all you need is to convert strings to numbers. 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 which would erase your home directory.

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

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 object or the module:

>>> importio>>> s="Hello, world">>> sio=io.StringIO(s)>>> sio.getvalue()'Hello, world'>>> sio.seek(7)7>>> sio.write("there!")6>>> sio.getvalue

Leave a Comment


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