<jupyter_start><jupyter_text>[OOP I: Introduction to Object Oriented Programming](https://lectures.quantecon.org/py/oop_intro.html) 1. Overview 2. Objects Type  The type of an object can be queried via `type(object_name)`<jupyter_code>s = 'This is a string'
type(s)
x = 42   # Now let's create an integer
type(x)
'300' + 'cc'  # 字符串连接
300 + 400    # 数值相加
'300' + 400  # 同类型
int('300') + 400   # To add as numbers, change the string to an integer<jupyter_output><empty_output><jupyter_text>IdentityThe identity of an object can be obtained via the `id()` function<jupyter_code>y = 2.5
z = 2.5
id(y)
id(z)<jupyter_output><empty_output><jupyter_text>Object Content: Data and AttributesIf we set `x = 42` then we create an object of type `int` that contains the data `42`<jupyter_code>x = 42
x
x.imag        # 虚数部分<jupyter_output><empty_output><jupyter_text>A Python complex number `z` is stored internally using rectangular or Cartesian coordinates. It is completely determined by its real part `z.real` and its imaginary part `z.imag`.<jupyter_code>x.__class__   # 类属性<jupyter_output><empty_output><jupyter_text>Methods  Methods are *functions that are bundled with objects*  `callable(object)`  检查对象object是否可调用。如果返回True，object仍然可能调用失败；但如果返回False，调用对象ojbect绝对不会成功。<jupyter_code>x = ['foo', 'bar']
callable(x.append)
callable(x.__doc__)
x = ['a', 'b']
x.append('c')
s = 'This is a string'
s.upper()
s.lower()
s.replace('This', 'That')
x = ['a', 'b']
x[0] = 'aa'  # Item assignment using square bracket notation
x<jupyter_output><empty_output><jupyter_text>What actually happens is that Python calls the `__setitem__` method, as follows<jupyter_code>x = ['a', 'b']
x.__setitem__(0, 'aa')  # Equivalent to x[0] = 'aa'
x<jupyter_output><empty_output><jupyter_text>3. SummaryIn Python, everything in memory is treated as an objectThis includes not just lists, strings, etc., but also less obvious things, such as-    functions (once they have been read into memory)-    modules (ditto)-    files opened for reading or writing-    integers, etc.<jupyter_code>def f(x): return x**2
f
type(f)
id(f)
f.__name__<jupyter_output><empty_output><jupyter_text>We can see that `f` has type, identity, attributes and so on—just like any other objectIt also has methodsOne example is the `__call__` method, which just evaluates the function<jupyter_code>f.__call__(3)<jupyter_output><empty_output><jupyter_text>Another is the `__dir__` method, which returns a list of attributesModules loaded into memory are also treated as objects<jupyter_code>print(f.__dir__)
import math

id(math)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11372397841171936, 'fraction_numerical': 0.015805705474171164, 'mean_word_length': 5.883289124668435, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '331314', 'license': 'MIT', 'n_tokens_mistral': 967, 'n_tokens_neox': 935, 'n_words': 280, 'path': '1.5 OOP I Introduction to Object Oriented Programming.ipynb', 'repo_name': 'gurusmile/QuantEcon', 'size': '2594'}