Zanurkuj w Pythonie/locals i globals: Różnice pomiędzy wersjami

Usunięta treść Dodana treść
Piotr (dyskusja | edycje)
zaczynam tłumaczyć
Piotr (dyskusja | edycje)
Nie podano opisu zmian
Linia 13:
Nie, czekaj, nie mogłeś jeszcze się uczyć o <tt>locals</tt>. Najpierw, musisz nauczyć się, czym są przestrzenie nazw. Przedstawimy teraz trochę suchego materiału, lecz ważnego, dlatego też zachowaj uwagę.
 
Python korzysta z czegoś, co się nazywa przestrzenią nazw (ang. ''namespace''), aby śledzić zmienne. Przestrzeń nazw jest właśnie jak słownik, gdzie kluczami są nazwy zmienne, a wartościami słownika są wartości tych zmiennych. Właściwie możesz dostać się do przestrzeni nazw jak do Pythonowego słownika, co zresztą zobaczymy za chwilkę.
Python uses what are called namespaces to keep track of variables. A namespace is just like a dictionary where the keys are names of variables and the dictionary values are the values of those variables. In fact, you can access a namespace as a Python dictionary, as you'll see in a minute.
 
Z dowolnego miejsca Pythonowego programu mamy dostęp do kliku przestrzeni nazw. Każda funkcja posiada własną przestrzeń nazw, nazywaną ''lokalną przestrzenią nazw'', a która śledzi zmienne funkcji, włączając to jej argumenty i lokalnie zdefiniowane zmienne. Każdy moduł posiada własną przestrzeń nazw, nazwaną ''globalną przestrzenią nazw'', a która śledzi zmienne modułu, włączając to funkcje, klasy i inne zaimportowane moduły, a także zmienne zdefiniowane w tym module i stałe. Jest także wbudowana przestrzeń nazw, dostępna z każdego modułu, a która przechowuje funkcje wbudowane i wyjątki.
At any particular point in a Python program, there are several namespaces available. Each function has its own namespace, called the local namespace, which keeps track of the function's variables, including function arguments and locally defined variables. Each module has its own namespace, called the global namespace, which keeps track of the module's variables, including functions, classes, any other imported modules, and module-level variables and constants. And there is the built-in namespace, accessible from any module, which holds built-in functions and exceptions.
 
Kiedy pewna linia kodu pyta się o wartość zmiennej <tt>x</tt>, Python przeszuka wszystkie przestrzenie nazw, aby znaleść ją, w poniższym porządku:
When a line of code asks for the value of a variable x, Python will search for that variable in all the available namespaces, in order:
# lokalna przestrzeń nazw - określona dla bieżącej funkcji lub metody pewnej klasy. Jeśli funkcja definiuje jakąś lokalną zmienną <tt>x</tt>, Python wykorzysta ją i zakończy szukanie.
 
# globalna przestrzeń nazw - określona dla bieżącego modułu. Jeśli moduł definiuje zmienną lub klasę o nazwie <tt>x</tt>, Python wykorzysta ją i zakończy szukanie.
# local namespace - specific to the current function or class method. If the function defines a local variable x, or has an argument x, Python will use this and stop searching.
# wbudowana przestrzeń nazw namespace - globalna dla wszystkich modułów. Ponieważ jest to ostatnia deska ratunku, Python przyjmie, że <tt>x</tt> jest nazwą wbudowanej funkcji lub zmiennej.
# global namespace - specific to the current module. If the module has defined a variable, function, or class called x, Python will use that and stop searching.
# built-in namespace - global to all modules. As a last resort, Python will assume that x is the name of built-in function or variable.
 
If Python doesn't find x in any of these namespaces, it gives up and raises a NameError with the message There is no variable named 'x', which you saw back in Example 3.18, “Referencing an Unbound Variable”, but you didn't appreciate how much work Python was doing before giving you that error.