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
与类和实例无绑定关系的function都属于函数(function)
与类和实例有绑定关系的function都属于方法(method)
与类和实例有绑定关系的function都属于方法(method)
2018-04-25
注意self参数已在super()中传入,在__init__()中将隐式传递,不需要写出(也不能写)
注意过的,依然在此处出问题了
注意过的,依然在此处出问题了
2018-04-25