Created
November 8, 2011 13:03
-
-
Save jackywyz/1347688 to your computer and use it in GitHub Desktop.
Python练习
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def abstract(func): | |
func.__isabstract__ = True | |
return func | |
class Abstract(type): | |
def __new__(metaclz, definedclzname, supers, attrs): | |
clz = type.__new__(metaclz, definedclzname, supers, attrs) | |
# 這個類別自己定義的抽象方法 | |
abstracts = {name | |
for name, value in attrs.items() | |
if getattr(value, "__isabstract__", False)} | |
# 從父類別中繼承下來的抽象方法 | |
for super in supers: | |
for name in getattr(super, "__abstractmethods__", set()): | |
value = getattr(clz, name, None) | |
# 如果類別有定義自己的方法,則該方法就不會有 __isabstract__ | |
# 就不會被加入 abstracts | |
if getattr(value, "__isabstract__", False): | |
abstracts.add(name) | |
# 這個類別總共的抽象方法 | |
clz.__abstractmethods__ = frozenset(abstracts) | |
return clz | |
class Abs(object): | |
__metaclass__ = Abstract | |
@abstract | |
def doSome(): | |
pass | |
class SubAbs(Abs): | |
def doSome(): | |
pass | |
sub = SubAbs() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sidedish(number): | |
return { | |
1 : lambda meal: (lambda: meal() + 30), | |
2 : lambda meal: (lambda: meal() + 40), | |
3 : lambda meal: (lambda: meal() + 50), | |
4 : lambda meal: (lambda: meal() + 60) | |
}.get(number, lambda meal: (lambda: meal())) | |
@sidedish(2) | |
@sidedish(3) | |
def friedchicken(): # friedchicken = sidedish(2)(sidedish(3)(friedchicken)) | |
return 49.0 | |
print(friedchicken()) # 139.0 | |
#多选操作 | |
def sayweek(num):return {1:'monday',2:'tuesday'}.get(num,'none') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#1. | |
class MyDict(object): | |
pass | |
#2. | |
class MyNewStyle1: | |
__metaclass__ = type | |
#3.设定一个模块级的__metaclass__ | |
__metaclass__ = type |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1) python多继承是广度优先查询重写的方法的。 | |
2) __base__ 表示该类继承的父类。可修改。 | |
3) 类和实例的 __dict__属性 存放对应的成员。实例存放初始化的参数。可通过vars方法获得__dict__的值。 | |
4) _xxx 不能用'from module import *'导入 | |
__xxx__ 系统定义名字 | |
__xxx 类中的私有变量名 | |
5) 继承的子类,可以通过super(A,self).__init__()调用父类方法。 | |
6)类定义 __call__()方法,则s=Some() ,可以 s(1)操作调用__call__()方法. | |
7) Some.__call__()将调用__new__和__init__完成类的构建 | |
8) 定义的metaclass 类 被创建的时候就调用 type子类的 __new__和__init__ | |
9) __slots__=['a','b']限制只能初始化名为 a,b的参数 | |
10) def say(fun):return fun.func_name | |
11) 切片ab[1:3:2] (在有三个参数的情况下,第一个起始位置,第二个是终止位置,地三个是步长) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
`1. 如果重写了__new__而在__new__里面没有调用__init__那么__init__将不起作用。 | |
`2. 可以这样理解,默认是创建(__new__),然后调用__init__ | |
`3. __call__定义再类中,则该类对象可以别调用。eg: s=Some(),s(2)是调用__call__方法。 | |
def __call__(cls, *args, **kwargs): | |
result = cls.__new__(cls, *args, **kwargs) | |
if isinstance(result, cls): | |
type(result).__init__(result,*args,**kwargs) | |
return result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Property(object): | |
"Emulate PyProperty_Type() in Objects/descrobject.c" | |
def __init__(self, fget=None, fset=None, fdel=None, doc=None): | |
self.fget = fget | |
self.fset = fset | |
self.fdel = fdel | |
self.__doc__ = doc | |
def __get__(self, obj, objtype=None): | |
if obj is None: | |
return self | |
if self.fget is None: | |
raise AttributeError, "unreadable attribute" | |
return self.fget(obj) | |
def __set__(self, obj, value): | |
if self.fset is None: | |
raise AttributeError, "can't set attribute" | |
self.fset(obj, value) | |
def __delete__(self, obj): | |
if self.fdel is None: | |
raise AttributeError, "can't delete attribute" | |
self.fdel(obj) | |
#静态方法 | |
class staticmth: | |
def __init__(self, mth): | |
self.mth = mth | |
def __get__(self, instance, owner): | |
return self.mth | |
class Some: | |
@staticmth # 相當於 doIt = staticmth(doIt) | |
def doIt(a, b): | |
print(a, b) | |
#静态类方法 | |
class classmth: | |
def __init__(self, mth): | |
self.mth = mth | |
def __get__(self, instance, owner): | |
def wrapper(*arg, **kwargs): | |
return self.mth(owner, *arg, **kwargs) | |
return wrapper | |
class Other: | |
@classmth # 相當於 doIt = classmth(doIt) | |
def doIt(clz, a, b): | |
print(clz, a, b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment