def is_sqr(x):
for i in range(1,11):
if x== i*i:
return x
print filter(is_sqr, range(1, 101))
for i in range(1,11):
if x== i*i:
return x
print filter(is_sqr, range(1, 101))
2015-05-09
属性不就是实现了set方法和get方法的实例变量吗?我认为老师讲的思路没有错,一楼的说法只能是作为一个记忆性理解,@property本质上就是装饰器,使函数实现set方法和get方法
2015-05-09
最后是显示2还是显示2/1我觉得都可以吧,2/1能够反应数据类型,2更加符合常识:
def __str__(self):
if self.q == 1:
return '%s' % self.p
else:
return '%s/%s' % (self.p,self.q)
def __str__(self):
if self.q == 1:
return '%s' % self.p
else:
return '%s/%s' % (self.p,self.q)
2015-05-09
def simple(self):
for i in range(int(max(self.p,self.q)**1/2)):
if ( self.p % (i + 2) == 0 ) and ( self.q % (i + 2) == 0 ):
self.p = self.p / (i+2)
self.q = self.q / (i+2)
return self
for i in range(int(max(self.p,self.q)**1/2)):
if ( self.p % (i + 2) == 0 ) and ( self.q % (i + 2) == 0 ):
self.p = self.p / (i+2)
self.q = self.q / (i+2)
return self
2015-05-09
如果list中含有其他的类,则应该先检查类是否一致:
def __cmp__(self, s):
if s.__class__ != self.__class__:
return 0
elif self.name < s.name:
return -1
elif self.name > s.name:
return 1
elif self.name == s.name:
return 0
def __cmp__(self, s):
if s.__class__ != self.__class__:
return 0
elif self.name < s.name:
return -1
elif self.name > s.name:
return 1
elif self.name == s.name:
return 0
2015-05-09
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
已采纳回答 / qq_侍_0
6章第一节的视频里就说了。类中有关联性的特殊方法__getattr__,__setattr__,__delattr__,都必须实现,那么get/set两者也是关联性方法,所以用@property装饰get方法,那么就会延伸一个可以装饰set方法的装饰器那也就是所说的副产品
2015-05-06
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