Python .local()

BrandonDusch's avatar
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.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

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:

Code
Output

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:

Code
Output

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours