add class study ipny file

This commit is contained in:
Lostecho
2023-06-15 16:42:53 +08:00
parent 9cd267edbf
commit 732b606e10
2 changed files with 828 additions and 1 deletions

2
.gitignore vendored
View File

@@ -157,4 +157,4 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.idea/

827
class_test/class.ipynb Normal file
View File

@@ -0,0 +1,827 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "726843f4-99a4-41bc-8d93-af525dc038a3",
"metadata": {
"ExecuteTime": {
"end_time": "2023-06-15T08:01:53.459031800Z",
"start_time": "2023-06-15T08:01:53.428235200Z"
}
},
"outputs": [
{
"data": {
"text/plain": "'Clay'"
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "2023"
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "<bound method Golem.say_hi of <__main__.Golem object at 0x000001CA0C48C2D0>>"
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hi!\n"
]
},
{
"data": {
"text/plain": "__main__.Golem"
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "str"
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "int"
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "method"
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "method"
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.core.interactiveshell import InteractiveShell\n",
"InteractiveShell.ast_node_interactivity = \"all\"\n",
"import datetime\n",
"\n",
"class Golem:\n",
"\n",
" def __init__(self, name=None):\n",
" self.name = name\n",
" self.built_year = datetime.date.today().year\n",
"\n",
" def say_hi(self):\n",
" print('Hi!')\n",
"\n",
"g = Golem('Clay')\n",
"g.name\n",
"g.built_year\n",
"g.say_hi\n",
"g.say_hi()\n",
"type(g)\n",
"type(g.name)\n",
"type(g.built_year)\n",
"type(g.__init__)\n",
"type(g.say_hi)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"outputs": [
{
"data": {
"text/plain": "<bound method Running_Golem.run of <__main__.Running_Golem object at 0x000001CA0CF9A210>>"
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Can't you see? I'm running...\n"
]
},
{
"data": {
"text/plain": "'Clay'"
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "2023"
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hi!\n"
]
}
],
"source": [
"from IPython.core.interactiveshell import InteractiveShell\n",
"InteractiveShell.ast_node_interactivity = \"all\"\n",
"import datetime\n",
"\n",
"class Golem:\n",
"\n",
" def __init__(self, name=None):\n",
" self.name = name\n",
" self.built_year = datetime.date.today().year\n",
"\n",
" def say_hi(self):\n",
" print('Hi!')\n",
"\n",
"class Running_Golem(Golem):\n",
"\n",
" def run(self):\n",
" print(\"Can't you see? I'm running...\")\n",
"\n",
"rg = Running_Golem('Clay')\n",
"\n",
"rg.run\n",
"rg.run()\n",
"rg.name\n",
"rg.built_year\n",
"rg.say_hi()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-06-15T08:06:54.529442700Z",
"start_time": "2023-06-15T08:06:54.509561100Z"
}
}
},
{
"cell_type": "code",
"execution_count": 5,
"outputs": [
{
"data": {
"text/plain": "<bound method Running_Golem.run of <__main__.Running_Golem object at 0x000001CA0CFC7790>>"
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Can't you see? I'm running...\n"
]
},
{
"data": {
"text/plain": "'Clay'"
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "2023"
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hi! Nice day, Huh?\n"
]
}
],
"source": [
"from IPython.core.interactiveshell import InteractiveShell\n",
"InteractiveShell.ast_node_interactivity = \"all\"\n",
"import datetime\n",
"\n",
"class Golem:\n",
"\n",
" def __init__(self, name=None):\n",
" self.name = name\n",
" self.built_year = datetime.date.today().year\n",
"\n",
" def say_hi(self):\n",
" print('Hi!')\n",
"\n",
"class Running_Golem(Golem):\n",
"\n",
" def run(self):\n",
" print(\"Can't you see? I'm running...\")\n",
"\n",
" def say_hi(self):\n",
" print('Hi! Nice day, Huh?')\n",
"\n",
"rg = Running_Golem('Clay')\n",
"\n",
"rg.run\n",
"rg.run()\n",
"rg.name\n",
"rg.built_year\n",
"rg.say_hi()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-06-15T08:08:02.232573600Z",
"start_time": "2023-06-15T08:08:02.142092Z"
}
}
},
{
"cell_type": "code",
"execution_count": 6,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on Running_Golem in module __main__ object:\n",
"\n",
"class Running_Golem(Golem)\n",
" | Running_Golem(name=None)\n",
" | \n",
" | Method resolution order:\n",
" | Running_Golem\n",
" | Golem\n",
" | builtins.object\n",
" | \n",
" | Methods defined here:\n",
" | \n",
" | run(self)\n",
" | \n",
" | say_hi(self)\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Methods inherited from Golem:\n",
" | \n",
" | __init__(self, name=None)\n",
" | Initialize self. See help(type(self)) for accurate signature.\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Data descriptors inherited from Golem:\n",
" | \n",
" | __dict__\n",
" | dictionary for instance variables (if defined)\n",
" | \n",
" | __weakref__\n",
" | list of weak references to the object (if defined)\n",
"\n"
]
},
{
"data": {
"text/plain": "['__class__',\n '__delattr__',\n '__dict__',\n '__dir__',\n '__doc__',\n '__eq__',\n '__format__',\n '__ge__',\n '__getattribute__',\n '__getstate__',\n '__gt__',\n '__hash__',\n '__init__',\n '__init_subclass__',\n '__le__',\n '__lt__',\n '__module__',\n '__ne__',\n '__new__',\n '__reduce__',\n '__reduce_ex__',\n '__repr__',\n '__setattr__',\n '__sizeof__',\n '__str__',\n '__subclasshook__',\n '__weakref__',\n 'built_year',\n 'name',\n 'run',\n 'say_hi']"
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "{'name': 'Clay', 'built_year': 2023}"
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "True"
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.core.interactiveshell import InteractiveShell\n",
"InteractiveShell.ast_node_interactivity = \"all\"\n",
"import datetime\n",
"\n",
"class Golem:\n",
"\n",
" def __init__(self, name=None):\n",
" self.name = name\n",
" self.built_year = datetime.date.today().year\n",
"\n",
" def say_hi(self):\n",
" print('Hi!')\n",
"\n",
"class Running_Golem(Golem):\n",
"\n",
" def run(self):\n",
" print('Can\\'t you see? I\\'m running...')\n",
"\n",
" def say_hi(self):\n",
" print('Hi! Nice day, Huh?')\n",
"\n",
"rg = Running_Golem('Clay')\n",
"\n",
"help(rg)\n",
"dir(rg)\n",
"rg.__dict__\n",
"hasattr(rg, 'built_year')"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-06-15T08:09:48.581157600Z",
"start_time": "2023-06-15T08:09:48.562887800Z"
}
}
},
{
"cell_type": "code",
"execution_count": 8,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "True"
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "False"
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "False"
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "False"
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "1"
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "10"
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "11"
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "10"
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "10"
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "True"
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.core.interactiveshell import InteractiveShell\n",
"InteractiveShell.ast_node_interactivity = \"all\"\n",
"import datetime\n",
"\n",
"class Golem:\n",
" population = 0\n",
" __life_span = 10\n",
"\n",
" def __init__(self, name=None):\n",
" self.name = name\n",
" self.built_year = datetime.date.today().year\n",
" self.__active = True\n",
" Golem.population += 1\n",
"\n",
" def say_hi(self):\n",
" print('Hi!')\n",
"\n",
" def cease(self):\n",
" self.__active = False\n",
" Golem.population -= 1\n",
"\n",
" def is_active(self):\n",
" if datetime.date.today().year - self.built_year >= Golem.__life_span:\n",
" self.cease()\n",
" return self.__active\n",
"\n",
"g = Golem()\n",
"hasattr(Golem, 'population')\n",
"hasattr(g, 'population')\n",
"hasattr(Golem, '__life_span')\n",
"hasattr(g, '__life_span')\n",
"hasattr(g, '__active')\n",
"Golem.population\n",
"setattr(Golem, 'population', 10)\n",
"Golem.population\n",
"x = Golem()\n",
"Golem.population\n",
"x.cease()\n",
"Golem.population\n",
"getattr(g, 'population')\n",
"g.is_active()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-06-15T08:18:02.767773400Z",
"start_time": "2023-06-15T08:18:02.712793300Z"
}
}
},
{
"cell_type": "code",
"execution_count": 11,
"outputs": [
{
"data": {
"text/plain": "<bound method Golem.population of <__main__.Golem object at 0x000001CA0D02D5D0>>"
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "1"
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.core.interactiveshell import InteractiveShell\n",
"InteractiveShell.ast_node_interactivity = \"all\"\n",
"import datetime\n",
"\n",
"class Golem:\n",
" __population = 0\n",
" __life_span = 10\n",
"\n",
" def __init__(self, name=None):\n",
" self.name = name\n",
" self.built_year = datetime.date.today().year\n",
" self.__active = True\n",
" Golem.__population += 1\n",
"\n",
" def say_hi(self):\n",
" print('Hi!')\n",
"\n",
" def cease(self):\n",
" self.__active = False\n",
" Golem.__population -= 1\n",
"\n",
" def is_active(self):\n",
" if datetime.date.today().year - self.built_year >= Golem.__life_span:\n",
" self.cease()\n",
" return self.__active\n",
"\n",
" def population(self):\n",
" return Golem.__population\n",
"\n",
"g = Golem('Clay')\n",
"g.population\n",
"g.population()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-06-15T08:24:16.538713500Z",
"start_time": "2023-06-15T08:24:16.524642600Z"
}
}
},
{
"cell_type": "code",
"execution_count": 13,
"outputs": [
{
"data": {
"text/plain": "<property at 0x1ca0d0484f0>"
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.core.interactiveshell import InteractiveShell\n",
"InteractiveShell.ast_node_interactivity = \"all\"\n",
"import datetime\n",
"\n",
"class Golem:\n",
" __population = 0\n",
" __life_span = 10\n",
"\n",
" def __init__(self, name=None):\n",
" self.name = name\n",
" self.built_year = datetime.date.today().year\n",
" self.__active = True\n",
" Golem.__population += 1\n",
"\n",
" def say_hi(self):\n",
" print('Hi!')\n",
"\n",
" def cease(self):\n",
" self.__active = False\n",
" Golem.population -= 1\n",
"\n",
" def is_active(self):\n",
" if datetime.date.today().year - self.built_year >= Golem.__life_span:\n",
" self.cease()\n",
" return self.__active\n",
"\n",
" @property\n",
" def population(self):\n",
" return Golem.population\n",
"\n",
"g = Golem('Clay')\n",
"g.population\n",
"# g.population = 100"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-06-15T08:29:09.532026600Z",
"start_time": "2023-06-15T08:29:09.478366200Z"
}
}
},
{
"cell_type": "code",
"execution_count": 14,
"outputs": [
{
"data": {
"text/plain": "<property at 0x1ca0c1d13f0>"
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "<property at 0x1ca0c1d13f0>"
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "<property at 0x1ca0c1d13f0>"
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on class Golem in module __main__:\n",
"\n",
"class Golem(builtins.object)\n",
" | Golem(name=None)\n",
" | \n",
" | Methods defined here:\n",
" | \n",
" | __init__(self, name=None)\n",
" | Initialize self. See help(type(self)) for accurate signature.\n",
" | \n",
" | cease(self)\n",
" | \n",
" | is_active(self)\n",
" | \n",
" | say_hi(self)\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Data descriptors defined here:\n",
" | \n",
" | __dict__\n",
" | dictionary for instance variables (if defined)\n",
" | \n",
" | __weakref__\n",
" | list of weak references to the object (if defined)\n",
" | \n",
" | population\n",
"\n"
]
},
{
"data": {
"text/plain": "mappingproxy({'__module__': '__main__',\n '_Golem__population': 101,\n '_Golem__life_span': 10,\n '__init__': <function __main__.Golem.__init__(self, name=None)>,\n 'say_hi': <function __main__.Golem.say_hi(self)>,\n 'cease': <function __main__.Golem.cease(self)>,\n 'is_active': <function __main__.Golem.is_active(self)>,\n 'population': <property at 0x1ca0c1d13f0>,\n '__dict__': <attribute '__dict__' of 'Golem' objects>,\n '__weakref__': <attribute '__weakref__' of 'Golem' objects>,\n '__doc__': None})"
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "{'name': 'Clay', 'built_year': 2023, '_Golem__active': True}"
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "True"
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "<property at 0x1ca0c1d13f0>"
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "10000"
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.core.interactiveshell import InteractiveShell\n",
"InteractiveShell.ast_node_interactivity = \"all\"\n",
"import datetime\n",
"\n",
"class Golem:\n",
" __population = 0\n",
" __life_span = 10\n",
"\n",
" def __init__(self, name=None):\n",
" self.name = name\n",
" self.built_year = datetime.date.today().year\n",
" self.__active = True\n",
" Golem.__population += 1\n",
"\n",
" def say_hi(self):\n",
" print('Hi!')\n",
"\n",
" def cease(self):\n",
" self.__active = False\n",
" Golem.__population -= 1\n",
"\n",
" def is_active(self):\n",
" if datetime.date.today().year - self.built_year >= Golem.__life_span:\n",
" self.cease()\n",
" return self.__active\n",
"\n",
" @property\n",
" def population(self):\n",
" return Golem.population\n",
"\n",
" @population.setter\n",
" def population(self, value):\n",
" Golem.__population = value\n",
"\n",
"g = Golem('Clay')\n",
"g.population\n",
"g.population = 100\n",
"ga = Golem('New')\n",
"g.population\n",
"ga.population\n",
"help(Golem)\n",
"Golem.__dict__\n",
"g.__dict__\n",
"hasattr(Golem, 'population')\n",
"getattr(Golem, 'population')\n",
"setattr(Golem, 'population', 10000)\n",
"g.population"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-06-15T08:29:27.143013600Z",
"start_time": "2023-06-15T08:29:27.066281400Z"
}
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false
}
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}