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

Usunięta treść Dodana treść
Piotr (dyskusja | edycje)
Nie podano opisu zmian
Piotr (dyskusja | edycje)
m uciąłem trochę tę uwagę, dotyczyła starszych wersji pythona, tłumaczenie
Linia 22:
# 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.
 
IfJeśli Python doesn'tnie findznajdzie <tt>x</tt> inw anyżadnej ofz thesetych namespacesprzestrzeni nazw, itpoddaje givessię upi andwyrzuci raises awyjątek NameError withz thewiadomością message There''„There is no variable named 'x'”'', whichktóry youzobaczyliśmy saww back in Example''Przykładzie 3.18'', “Referencing„Odwoływanie ansię Unbounddo Variable”niezdefiniowanej zmiennej”, butlecz younie didn'tjesteś appreciatew howstanie muchocenić, workjak Python was doing beforezadziała, givingzanim youdostaniesz thatten errorbłąd.
 
{{Uwaga|
Important
Python 2.2 wprowadził subtelną, a zarazem ważną zmianę, które wpływa na porządek przeszukiwania przestrzeni nazw: zagnieżdżona przestrzeń nazw (ang. ''nested scope''). W wersjach wcześniejszych niż 2.2, kiedy odwoływało się do zmiennej wewnątrz zagnieżdżonej funkcji lub funkcji lambda, Python będzie szukał tej zmiennej w bieżącej przestrzeni nazw danej funkcji (zagnieżdżonej lub lambda), a następnie w przestrzeni nazw modułu. Python 2.2 będzie szukał zmiennej w bieżącej przestrzeni nazw danej funkcji (zagnieżdżonej lub lambda), następnie w przestrzeni nazw nadrzędnej funkcji, a potem w przestrzeni nazw modułu.
Python 2.2 introduced a subtle but important change that affects the namespace search order: nested scopes. In versions of Python prior to 2.2, when you reference a variable within a nested function or lambda function, Python will search for that variable in the current (nested or lambda) function's namespace, then in the module's namespace. Python 2.2 will search for the variable in the current (nested or lambda) function's namespace, then in the parent function's namespace, then in the module's namespace. Python 2.1 can work either way; by default, it works like Python 2.0, but you can add the following line of code at the top of your module to make your module work like Python 2.2:
}}
 
Zmieszałeś się? Nie panikuj! Jest to naprawdę cool. Podobnie, jak wiele rzeczy w Pythonie, przestrzenie nazw są bezpośrednio dostępne podczas wykonywania programu. Jak? Do lokalnej przestrzeni nazw mamy dostęp poprzez wbudowaną funkcję <tt>locals</tt>, a do globalna (na poziomie modułu) przestrzeń nazw jest dostępna poprzez wbudowaną funkcję <tt>globals</tt>.
 
'''Przykład 8.10. Wprowadzenie do <tt>locals</tt>'''
from __future__ import nested_scopes
 
Are you confused yet? Don't despair! This is really cool, I promise. Like many things in Python, namespaces are directly accessible at run-time. How? Well, the local namespace is accessible via the built-in locals function, and the global (module level) namespace is accessible via the built-in globals function.
 
'''Example 8.10. Introducing locals'''
 
>>> def foo(arg): #(1)
Linia 43 ⟶ 41:
{'arg': 'bar', 'x': 1}
 
# Funkcja <tt>foo</tt> posiada dwie zmienne w swojej lokalnej przestrzeni nazw: <tt>arg</tt>, której wartość jest przekazana do funkcji, a także <tt>x</tt>, która jest zdefiniowana wewnątrz funkcji.
# The function foo has two variables in its local namespace: arg, whose value is passed in to the function, and x, which is defined within the function.
# <tt>locals</tt> zwraca słownik par nazwa/wartość. Kluczami słownika są nazwy zmiennej, a które są napisami. Wartościami słownika są bieżące wartości tych zmiennych. Zatem wywołując <tt>foo</tt> z 7, wypiszemy słownik zawierający dwie lokalne zmienne tej funkcji, czyli <tt>arg</tt> (o wartości <tt>7</tt>) i <tt>x</tt> (o wartości <tt>1</tt>).
# locals returns a dictionary of name/value pairs. The keys of this dictionary are the names of the variables as strings; the values of the dictionary are the actual values of the variables. So calling foo with 7 prints the dictionary containing the function's two local variables: arg (7) and x (1).
# Pamiętaj, Python jest dynamicznie typowany, dlatego też możemy w prosty sposób jako argument <tt>arg</tt>, przekazać napis. Funkcja (a także wywołanie <tt>locals</tt>) będą nadal działać jak należy. <tt>locals</tt> działa z wszystkimi zmiennymi dowolnych typów danych.
# Remember, Python has dynamic typing, so you could just as easily pass a string in for arg; the function (and the call to locals) would still work just as well. locals works with all variables of all datatypes.
 
What locals does for the local (function) namespace, globals does for the global (module) namespace. globals is more exciting, though, because a module's namespace is more exciting <ref>I don't get out much</ref>. Not only does the module's namespace include module-level variables and constants, it includes all the functions and classes defined in the module. Plus, it includes anything that was imported into the module.