class Person(object):
def __init__(self, name, gender, **kw):
self.name = name
self.gender = gender
for k, v in kw.items():
setattr(self, k, v)
p = Person('Bob', 'Male', age=18, course='Python')
print(p.age)
print(p.course)
def __init__(self, name, gender, **kw):
self.name = name
self.gender = gender
for k, v in kw.items():
setattr(self, k, v)
p = Person('Bob', 'Male', age=18, course='Python')
print(p.age)
print(p.course)
2018-01-02
class Person(object):
__count = 0
def __init__(self, name):
Person.__count = Person.__count + 1
self.name = name
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
print 'attributeerror'
__count = 0
def __init__(self, name):
Person.__count = Person.__count + 1
self.name = name
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
print 'attributeerror'
2018-01-02
class Person(object):
__count = 0
def __init__(self, name):
self.name = name
Person.__count = Person.__count + 1
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError:
print "attributeerror"
__count = 0
def __init__(self, name):
self.name = name
Person.__count = Person.__count + 1
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError:
print "attributeerror"
2018-01-02
count = 0
def __init__(self,name):
self.name = name
Person.count = Person.count + 1
def __init__(self,name):
self.name = name
Person.count = Person.count + 1
2018-01-02
L2 = sorted(L1,lambda p1,p2:cmp(p1.name.upper(),p2.name.upper()))
2018-01-02
def performance(f):
def fn(*args,**kw):
start = time.time()
r = f(*args,**kw)
end = time.time()
print "Call %s() in %fs" % (f.__name__,(end - start))
return r
return fn
def fn(*args,**kw):
start = time.time()
r = f(*args,**kw)
end = time.time()
print "Call %s() in %fs" % (f.__name__,(end - start))
return r
return fn
2018-01-02
import json
class Students(object):
def read(self):
return r'["Tim", "Bob", "Alice"]'
s = Students()
print json.load(s)
就是定义个read()
class Students(object):
def read(self):
return r'["Tim", "Bob", "Alice"]'
s = Students()
print json.load(s)
就是定义个read()
2018-01-02
class P1():
count = 0
class Person(P1):
def __init__(self,name):
self.name = name
P1.count += 1
count = 0
class Person(P1):
def __init__(self,name):
self.name = name
P1.count += 1
2018-01-02
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 AttributeError:
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 AttributeError:
print 'attributeError'
还要加异常。。。不看评论做不对啊
2018-01-02
def __init__(self,name,gender,birth,**kwargs):
self.name = name
self.gender = gender
self.birth = birth
for key,value in kwargs.items():
setattr(self,key,value)
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
self.name = name
self.gender = gender
self.birth = birth
for key,value in kwargs.items():
setattr(self,key,value)
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
2018-01-02