def format_name(s):
if isinstance(s,str):#----增强版-----
return s[:1].upper()+s[1:].lower()
print(list(map(format_name,['adam','LISA', 'barT'])))
if isinstance(s,str):#----增强版-----
return s[:1].upper()+s[1:].lower()
print(list(map(format_name,['adam','LISA', 'barT'])))
2018-01-14
讲解的时候能否把实例化方法,类方法,静态方法对比着一起讲一下,就这样的讲类方法,完全没明白呀,也不知道这三种方法的区别,谁可以调用,谁不能调用,在哪里使用
2018-01-14
import math
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, math.sqrt)
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, math.sqrt)
2018-01-14
p1 = Person('Bob', 90)
p2 = Person('Alice', 65)
p3 = Person('Tim', 48)
print p1.get_grade()
print p2.get_grade()
print p3.get_grade()
p2 = Person('Alice', 65)
p3 = Person('Tim', 48)
print p1.get_grade()
print p2.get_grade()
print p3.get_grade()
2018-01-13
def get_grade(self):
if (Person.__score[self.name] >= 90):
return 'A'
elif(Person.__score[self.name] >= 60):
return 'B'
else:
return 'C'
if (Person.__score[self.name] >= 90):
return 'A'
elif(Person.__score[self.name] >= 60):
return 'B'
else:
return 'C'
2018-01-13
class Person(object):
__score = {}
def __init__(self, name, score):
Person.__score[name] = score
self.name = name
__score = {}
def __init__(self, name, score):
Person.__score[name] = score
self.name = name
2018-01-13
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:
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:
print 'AttributeError'
2018-01-13
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-13