Объекты в Python
Введение | |
Передача аргументов | |
Изменяемые и неизменяемые объекты | |
Всё является объектом | |
Похожие статьи |
Введение
Основной объект в Python, от которого всё наследуется это PyObject
python.org
У него есть два атрибута:
счётчик ссылок
Py_REFCNT
и тип
Py_TYPE
.
Примеры
# id() print("# id():\n") a = 1961 print("a = 1961") print(f"id(a) = {id(a)}") b = 1965 print("b = 1065") print(f"id(b) = {id(b)}") b = a print("b = a") print(f"id(b) = {id(b)}") print(f"id(a) == id(b): {id(a) == id(b)}") print(f"a is b: {a is b}") i = 0 print("i = 0") print(f"id(i) = {id(i)}") i += 2 print("i += 2") # Создаётся новый объект print(f"id(i) = {id(i)}")
# id(): a = 1961 id(a) = 140042834389712 b = 1065 id(b) = 140042834389648 b = a id(b) = 140042834389712 id(a) == id(b): True a is b: True i = 0 id(i) = 140042835400976 i += 2 id(i) = 140042835401008
# Mutable Objects print("\n# Mutable Objects:\n") l = [0, 1, 2] print(f"l = {l}") s = l print("s = l") print(f"s = {s}") s[1] = 34 print("s[1] = 34") print(f"s = {s}") print(f"l = {l}") print(f"s is l: {s is l}") # Python doesn't have variables in the sense of boxes # holding a value # Python has named references to objects # Value vs. Identity Equality print("\n# Value vs. Identity Equality:\n") p = [5, 6, 7] print(f"p = {p}") q = [5, 6, 7] print(f"q = {q}") print(f"p == q: {p == q}") print(f"p is q: {p is q}") print(f"p is p: {p is p}") # Value-equality and identity equality are fundamentally # different concepts. # Comparison by value can be controlled programatically # Identity comparison is unalterable defined # by the language
python objects_example.py
# Mutable Objects: l = [0, 1, 2] s = l s = [0, 1, 2] s[1] = 34 s = [0, 34, 2] l = [0, 34, 2] s is l: True # Value vs. Identity Equality: p = [5, 6, 7] q = [5, 6, 7] p == q: True p is q: False p is p: True
Передача аргументов
m = [0, 1, 2] print(f"m = {m}") def modify(k): k.append(3) print("k =", k) print("\nmodify(m)\n") modify(m) print(f"m = {m}") print("\nNo copy of m is made\n") print("\nmodify(m)\n") modify(m) print(f"m = {m}")
m = [0, 1, 2] modify(m) k = [0, 1, 2, 3] m = [0, 1, 2, 3] No copy of m is made modify(m) k = [0, 1, 2, 3, 3] m = [0, 1, 2, 3, 3]
Функция modify() работает с тем же объектом, поэтому его значения изменяются.
Рассмотрим функцию replace() в которой создается новый объект
f = [4, 5, 6] print("f = ", f) def replace(g): g = [7, 8, 9] print("g =", g) print("\nreplace(f)\n") replace(f) print("f = ", f)
python replace.py
f = [4, 5, 6] replace(f) g = [7, 8, 9] f = [4, 5, 6]
Благодаря тому, что ссылка на старый объект не используется (вместо это переменная g указывает на новый объект) значение переменной f не изменилось.
Можно переписать функцию replace() так, чтобы она заменяла исходный объект.
Назовём новую функцию replace_contents()
def replace_contents(g): g[0] = 20 g[1] = 21 g[2] = 22 print("g = ", g) f = [10, 11, 12] print("f = ", f) print("\nreplace_contents(f)\n") replace_contents(f) print("f = ", f) # Function arguments are transferred using # pass-by-object-reference # References to objects are copied, # not the objects themselves.
python replace_contents.py
f = [10, 11, 12] replace_contents(f) g = [20, 21, 22] f = [20, 21, 22]
Убедиться в том, что если не произведено специальных действий, функция вернёт тот же самый объект довольно просто
def f(d): return d c = [0, 1, 2] print("c = ", c) e = f(c) print("e = f(c)") print(f"c is e: {c is e}")
python return_semantics.py
c = [0, 1, 2] e = f(c) c is e: True
Изменяемые и неизменяемые объекты
Объекты могут быть изменяемыми (mutable) и неизменяемыми (immutable)
Очень хорошей иллюстрацией разницы в их применении являются
аргументы по умолчанию
Множество (set) , Словарь (dict) и Список (list) это изменяемые объекты.
Всё является объектом
Рассмотрим скрипт words.py со следюущим содержанием.
"""Retrieve and print words from a URL. Usage: python3 words.py <URL> """ import sys from urllib.request import urlopen def fetch_words(url): """Fetch a list of words from a URL. Args: url: The URL of a UTF-8 text document. Returns: A list of strings containing the words from the document """ # PEP 257 # story = urlopen("http://sixty-north.com/c/t.txt") story = urlopen(url) story_words = [] for line in story: line_words = line.decode("utf8").split() for word in line_words: story_words.append(word) story.close() return story_words def print_items(story_words): """Print items one per line. Args: An iterable series of printable items. """ for word in story_words: print(word) def main(url): words = fetch_words(url) print_items(words) if __name__ == "__main__": main(sys.argv[1])
➭ python
Python 3.9.5 (default, Jun 15 2021, 15:30:04) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information.
>>> import words
>>> type(words)
<class 'module'>
>>> dir(words)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'fetch_words', 'main', 'print_items', 'sys', 'urlopen']
>>> type(words.fetch_words)
<class 'function'>
>>> dir(words.fetch_words)
['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
>>> words.fetch_words.__name__
'fetch_words'
>>> words.fetch_words.__doc__
'Fetch a list of words from a URL.\n \n Args:\n url: The URL of a UTF-8 text document.\n\n Returns:\n A list of strings containing the words from\n the document\n '
Основы Python | |
Python | |
Установка Python | |
ООП в Python | |
Функции | |
docstring | |
#!: Shebang | |
Объекты | |
Сложности при работе с Python |