Get the App
SLTechnology News&Howtos  ›  Development  › 

An example Analysis of the scope of Python variables

Shulou Source: shulou.com Published: 2022-06-02 08:47:25 10月03日 Update

The main content of this article is to explain "an example analysis of the scope of Python variables". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn the "Python variable scope example analysis" bar!

The variables in the function are local variables of the function and cannot be used outside the function.

> def f (): Xero5 > f () > > print (x) # error, x is not defined > def f () defines global variable x > f () > def 5 in print (x) 3 has no effect on the global variable

When the local variable has the same name as the global variable, the local variable masks the global variable

> Xerox 3 > def f (): Xero5 print (Xerox 2) > f () 25

If you want to use global variables and modify their values within the function first, use the keyword global to declare that global variables will be used

> Xerox 3 > def f (): global x print (Xerox variables 2) x examples 5 print (x cycles 2) > > f () 925 > x5 to this point, I believe you have a deeper understanding of the "Python variable scope example analysis", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Variables global function function analysis local content learning practical deeper key keyword interest practicality practical simple operation method more friends website Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Xiaomi Linux Redmi vpn