已采纳回答 / joe_cool
reduce 是python自带的一个函数 作用是将lst 从第1个开始 使用f函数进行连续操作 在这里就是指乘法操作 即如果lst = [1,2,3,4] 那么就是 计算 2 * 3 然后结果再乘以 4 。即 2 * 3 * 4
2018-04-27
def __init__(self, p, q):
self.p = p/__import__('fractions').gcd(p,q)
self.q = q/__import__('fractions').gcd(p,q)
self.p = p/__import__('fractions').gcd(p,q)
self.q = q/__import__('fractions').gcd(p,q)
2018-04-26
class Person(object):
def __init__(self, name, score):
self.name=name
self.__score=score
p = Person('Bob', 59)
print p.name
try:
print p.__score
except:
print 'attributeError'
def __init__(self, name, score):
self.name=name
self.__score=score
p = Person('Bob', 59)
print p.name
try:
print p.__score
except:
print 'attributeError'
2018-04-26
class Person(object):
def __init__(self, name, sex, birth, job):
self.name=name
self.sex=sex
self.birth=birth
self.job=job
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
print xiaoming.name
print xiaoming.job
def __init__(self, name, sex, birth, job):
self.name=name
self.sex=sex
self.birth=birth
self.job=job
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
print xiaoming.name
print xiaoming.job
2018-04-26
def format_name(s):
s = s.lower()
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
s = s.lower()
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-04-26
import math
def add(x, y, f):
return f(x) + f(y)
def sqrt(z):
return math.sqrt(z)
print add(25, 9, sqrt)
def add(x, y, f):
return f(x) + f(y)
def sqrt(z):
return math.sqrt(z)
print add(25, 9, sqrt)
2018-04-26
获取系统的当前日期:
temp_date=time.strftime('%Y-%m-%d',time.localtime(time.time()))
temp_date=time.strftime('%Y-%m-%d',time.localtime(time.time()))
2018-04-25
def __str__(self):
return '(Person: %s, %s)' % (self.name, self.gender)
__repr__ = __str__
先定义了__str__,之后又调用它复制,考虑此处的区别
return '(Person: %s, %s)' % (self.name, self.gender)
__repr__ = __str__
先定义了__str__,之后又调用它复制,考虑此处的区别
2018-04-25