Python 官方文档:入门教程 => 点击学习
get(self, instance, owner) 访问属性时调用 set(self, instance, value) 当对属性赋值时调用 delete(self, instance) 删除属性时调用 sel
删除属性时调用
描述器实现前提是描述器类实例作为类属性
注意所谓的类属性不仅仅类似与x=A()的属性,类中定义的函数也是类属性
from functools import partial
class StaticMethod:
def __init__(self, fn):
self.fn = fn
def __get__(self, instance, owner):
return self.fn
class ClassMethod:
def __init__(self, fn):
self.fn = fn
def __get__(self, instance, owner):
return partial(self.fn, owner)
class Test:
@StaticMethod
def s_mtd(): # s_mtd = StaticMethod(s_mtd)
print('s_mtd')
@ClassMethod
def c_mtd(cls): # c_mtd = ClassMethod(c_mtd)
print('c_mtd', cls)
if __name__ == '__main__':
Test.s_mtd()
Test.c_mtd()
class Property:
def __init__(self, fget=None, fset=None):
self.fget = fget
self.fset = fset
def __get__(self, instance, owner):
return self.fget(instance)
def __set__(self, instance, value):
self.fset(instance, value)
def getter(self):
pass
def setter(self, fset):
self.fset = fset
return self
class Person:
def __init__(self, name, age):
self._name = name
self._age = age
@Property
def name(self): # name=Property(name)
return self._name
@name.setter
def name(self, value): # name=Property(name).setter(name) (value)
self._name = value
@Property
def age(self): # name=Property(name)
return self._age
@age.setter
def age(self, value): # name=Property(name).setter(name) (value)
self._age = value
import inspect
class TypeCheck:
def __init__(self, key, type):
print('TC init')
self.key = key
self.type = type
def __get__(self, instance, owner):
print('TC get')
if instance is not None:
return instance.__dict__[self.key]
return self
def __set__(self, instance, value):
print('TC set')
if not isinstance(value, self.type):
raise TypeError
instance.__dict__[self.key] = value
def typeassert(cls):
params = inspect.signature(cls).parameters
for name, type in params.items():
if type != type.empty:
setattr(cls, name, type.annotation)
return cls
@typeassert
class Person:
name = TypeCheck('name', str)
age = TypeCheck('age', int)
def __init__(self, name: str, age: int):
self.name = name
self.age = age
tom = Person('tom', 12)
print(tom.name)
import inspect
class TypeCheck:
def __init__(self, key, type):
print('TC init')
self.key = key
self.type = type
def __get__(self, instance, owner):
print('TC get')
if instance is not None:
return instance.__dict__[self.key]
return self
def __set__(self, instance, value):
print('TC set')
if not isinstance(value, self.type):
raise TypeError
instance.__dict__[self.key] = value
class TypeAssert:
def __init__(self, cls):
self.cls = cls
def __call__(self, name, age):
params = inspect.signature(self.cls).parameters
for key, type in params.items():
if type != type.empty:
setattr(self.cls, key, TypeCheck(key, type.annotation))
return self.cls(name, age)
@TypeAssert
class Person: # Person = TypeAssert(Person)
name = TypeCheck('name', str)
age = TypeCheck('age', int)
def __init__(self, name: str, age: int):
self.name = name
self.age = age
tom = Person('tom', '12')
print(tom.name)
class node:
"""
Description: Node Class
attr item: current Node`s data
attr next: points to the next Node
attr past: points to the last Node
"""
def __init__(self, item: object):
self.__item = item
self.__next = None
self.__past = None
@property
def item(self):
return self.__item
@item.setter
def item(self, value):
self.__item = value
@property
def next(self):
return self.__next
@next.setter
def next(self, value: object):
self.__next = value
@property
def past(self):
return self.__past
@past.setter
def past(self, value: object):
self.__past = value
class LinkedList:
"""
Description: Base class LinkedList
"""
def __init__(self):
self.cur = None
self.head = None
self.length = 0
def append(self, no: object):
raise Exception('Base Method')
def iternodes(self):
raise Exception('Base Method')
def pop(self):
raise Exception('Base Method')
def insert(self, position: int, value: object):
raise Exception('Base Method')
def remove(self, value: object):
raise Exception('Base Method')
class SingleLinkedList(LinkedList):
"""
Description:
attr head: head Node
attr cur: current Node
method append(): append Node
"""
def __init__(self):
super().__init__()
def __iter__(self):
cur_node = self.head
while True:
yield cur_node.item
if not cur_node.next:
break
cur_node = cur_node.next
def __getitem__(self, item):
cur_node = self.head
if isinstance(item, slice):
pass
else:
for _ in range(item):
cur_node = cur_node.next
return cur_node.item
def __setitem__(self, key, value):
cur_node = self.head
for _ in range(key):
cur_node = cur_node.next
cur_node.item = value
def append(self, no: object):
if self.length == 0:
self.cur = Node(no)
self.head = self.cur
else:
self.cur.next = Node(no)
self.cur = self.cur.next
self.length += 1
sl = SingleLinkedList()
sl.append(1)
sl.append(2)
for i in sl:
print(i)
sl[1] = 999
sl[0] = 234
for i in sl:
print(i)
class DoubleLinkedList(LinkedList):
"""
Description:
attr head:
attr cur:
method append:
method pop:
method insert:
method remove:
method iternodes:
"""
def __init__(self):
super().__init__()
def __iter__(self):
cur_node = self.head
while True:
yield cur_node.item
if not cur_node.next:
break
cur_node = cur_node.next
def __reversed__(self):
cur_node = self.cur
while True:
yield cur_node.item
if not cur_node.past:
break
cur_node = cur_node.past
def __getitem__(self, item):
cur_node = self.head
if isinstance(item, slice):
pass
else:
for _ in range(item):
cur_node = cur_node.next
return cur_node.item
def __setitem__(self, key, value):
cur_node = self.head
for _ in range(key):
cur_node = cur_node.next
cur_node.item = value
def append(self, no: object):
if self.length == 0:
self.cur = Node(no)
self.head = self.cur
else:
temp = self.cur
self.cur.next = Node(no)
self.cur = self.cur.next
self.cur.past = temp
self.length += 1
def pop(self):
pop_node = self.cur
pop_node.past.next = None
self.cur = self.cur.past
self.length -= 1
return pop_node
def insert(self, position: int, value: object):
cur_node = self.head
new_node = Node(value)
for _ in range(position - 1):
cur_node = cur_node.next
next_node = cur_node.next
cur_node.next = new_node
new_node.past = cur_node
new_node.next = next_node
next_node.past = new_node
def remove(self, value: object):
cur_node = self.head
while True:
if cur_node.item == value:
cur_node.past.next = cur_node.next
cur_node.next.past = cur_node.past
break
elif not cur_node.next:
raise Exception('NodeNotFound')
cur_node = cur_node.next
def iternodes(self, *, reverse=False):
if not reverse:
cur_node = self.head
while True:
yield cur_node.item
if not cur_node.next:
break
cur_node = cur_node.next
else:
cur_node = self.cur
while True:
yield cur_node.item
if not cur_node.past:
break
cur_node = cur_node.past
raise 异常实例
try:
待捕获异常的代码块
except [异常类型] as e:
异常的处理代码块
else:
...
finally:
...
在import所在的作用域的局部命名空间中,增加名称和上一步创建的对象关联
from ... import *
setuptools
pip
pip install wheel
# from distutils.core import setup # 可能失败
from setuptools import setup
setup(name='Distutils',
version='1.0',
description='Python Distribution Utilities',
author='Greg Ward',
author_email='gward@python.net',
url='https://www.python.org/sigs/distutils-sig/',
packages=['distutils', 'distutils.command'],
)
python setup.py cmd -help
python setup.py build
python setup.py install
python setup.py sdist
useradd git
su - git
tar xf gogs*
cd gogs
mysql -uroot -p < scripts/mysql.sql
grant all on gogs.* to 'gogs'@'%' identified by 'gogs';
flush privileges;
mkdir -p custom/conf
cd custom/conf
touch app.ini
./gogs WEB
gogs下需要建立log目录
root用户操作
cp /home/git/gogs/scripts/init/Centos/gogs /etc/init.d/
chmod +x /etc/init.d/gogs
chkconfig gogs on
service gogs start
首次登陆
Http://IP:Port/install
--结束END--
本文标题: Python第九周 学习笔记(1)
本文链接: https://lsjlt.com/news/186962.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0