10.2 Pierwszy moduł

>>> pi
Traceback (innermost last):
  File "<stdin>", line 1, in ?
NameError: name 'pi' is not defined
>>> pole(2)
Traceback (innermost last):
  File "<stdin>", line 1, in ?
NameError: name 'pole' is not defined


>>> import matematyka
>>> pi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'pi' is not defined
>>> matematyka.pi
3.14159
>>> matematyka.pole(2)
12.56636
>>> matematyka.__doc__
'matematyka - przykładowy moduł matematyczny'
>>> matematyka.pole.__doc__
'pole(r): zwraca pole koła o promieniu r'


>>> from matematyka import pi
>>> pi
3.14159
>>> pole(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'pole' is not defined


>>> importlib.reload(matematyka)
<module 'matematyka' from 'C:\\Users\\matematyka.py'>


10.5 Nazwy prywatne w modułach

>>> from modtest import *
>>> f(3)
3
>>> _g(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name '_g' is not defined
>>> a
4
>>> _b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name '_b' is not defined

>>> import modtest
>>> modtest._b
2
>>> from modtest import _g
>>> _g(5)
5

10.7 Zasięg zmiennych i przestrzenie nazw w Pythonie

>>> locals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>}
>>> globals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>}


>>> z = 2
>>> import math
>>> from cmath import cos
>>> globals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'z': 2, 'math': <module 'math' (built-in)>, 'cos': <built-in function cos>}
>>> locals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'z': 2, 'math': <module 'math' (built-in)>, 'cos': <built-in function cos>}
>>> math.ceil(4)
4


>>> del z, math, cos
>>> locals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>}
>>> math.ceil(3.4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'math' is not defined
>>> import math
>>> math.ceil(3.4)
4


>>> def f(x):
...     print("globalnie: ", globals())
...     print("lokalnie: ", locals())
...     y = x
...     print("lokalnie: ", locals())
...
>>> z = 2
>>> globals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'math': <module 'math' (built-in)>, 'f': <function f at 0x00000203A81D3E18>, 'z': 2}
>>> f(z)
globalnie:  {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'math': <module 'math' (built-in)>, 'f': <function f at 0x00000203A81D3E18>, 'z': 2}
lokalnie:  {'x': 2}
lokalnie:  {'x': 2, 'y': 2}


>>> print(max.__doc__)
max(iterable, *[, default=obj, key=func]) -> value
max(arg1, arg2, *args, *[, key=func]) -> value

With a single iterable argument, return its biggest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the largest argument.


>>> list("Jezioro Wigry")
['J', 'e', 'z', 'i', 'o', 'r', 'o', ' ', 'W', 'i', 'g', 'r', 'y']
>>> list = [1, 3, 5, 6]
>>> list("Jezioro Wigry")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable


>>> import matematyka
>>> matematyka = matematyka.pole
>>> matematyka.pi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute 'pi'


>>> del list
>>> list("Jezioro Wigry")
['J', 'e', 'z', 'i', 'o', 'r', 'o', ' ', 'W', 'i', 'g', 'r', 'y']
>>> import matematyka
>>> matematyka.pi
3.14159


>>> x1 = 6
>>> xl = x1 - 2
>>> x1
6
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'matematyka', 'x1', 'xl']


