.local()

Published Jun 3, 2022
Contribute to Docs

The .local() method returns a local thread object with data that is specific to that thread. This data can be anything from arbitrary numbers to session-specific information like usernames.

Syntax

threading.local()

No parameters are used in the .local() method.

Example

Data can be locally stored as a property in a specific thread, as shown in the example below:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

The output would look like this:

1
2

Codebyte Example

Additionally, the object returned by .local() can be assigned to a variable, which is then stored in memory. Any stored data can be found in the local thread’s predefined __dict__ attribute:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn Python on Codecademy