dir(object)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
2015-05-08
动态语言的多态和静态语言c++等多态含义不太一样,c++中的多态就是参数数量和类型不同的方法就是不同方法,而动态语言中的多态其实值的是方法的寻找过程,即向右找到类(或者单件类),在类中找不到的话再找父类,一直在祖先链中找到或者找不到为止,先找到的就被调用:
class MyClass(object):
def fn(x):
print "This is the first fn"
def fn(x,y):
print "This is the second fn"
obj = MyClass()
obj.fn(1)
结果fn被复写,而没有实现多态
class MyClass(object):
def fn(x):
print "This is the first fn"
def fn(x,y):
print "This is the second fn"
obj = MyClass()
obj.fn(1)
结果fn被复写,而没有实现多态
2015-05-08
import math
def is_sqr(x):
return math.sqrt(x) % 1 == 0
print filter(is_sqr, range(1, 101))
都说要简单 这个怎么样。
def is_sqr(x):
return math.sqrt(x) % 1 == 0
print filter(is_sqr, range(1, 101))
都说要简单 这个怎么样。
2015-05-07
import json
class Students(object):
def __init__(self):
pass
def read(self):
return r'["tim", "Bob", "Alice"]'
s = Students()
print json.load(s)
class Students(object):
def __init__(self):
pass
def read(self):
return r'["tim", "Bob", "Alice"]'
s = Students()
print json.load(s)
2015-05-06
return abs(int(math.sqrt(x))-math.sqrt(x))<=0.0001
哈哈,这样应该不算投机取巧吧!
哈哈,这样应该不算投机取巧吧!
2015-05-05
super(Student, self).__init__(name, gender) 去初始化父类,我觉得这点这么说不合适,应该说通过这个简化了初始化self.name = name 和self.gender = gender ,有点难以说是初始化父类,而应该说是和父类一样初始化,简化了代码量。
2015-05-05
def count():
fs = []
for i in range(1, 4):
def f(j):
def g():
return j*j
return g
r=f(i)
print r()
fs.append(r)
print fs
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
原来fs里装的是函数名,不是值!
fs = []
for i in range(1, 4):
def f(j):
def g():
return j*j
return g
r=f(i)
print r()
fs.append(r)
print fs
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
原来fs里装的是函数名,不是值!
2015-05-03