def cmp_ignore_case(s1, s2):
return 1 if s1.upper()>s2.upper() else -1
return 1 if s1.upper()>s2.upper() else -1
2018-03-20
#coding:utf-8
def format_name(s):
#return s.title() #方法1
#return s.capitalize() #方法2
return s[0].upper() + s[1:].lower() #方法3
print map(format_name, ['adam', 'LISA', 'barT'])
def format_name(s):
#return s.title() #方法1
#return s.capitalize() #方法2
return s[0].upper() + s[1:].lower() #方法3
print map(format_name, ['adam', 'LISA', 'barT'])
2018-03-20
同时写init和call
class Fib(object):
def __init__(self):
self.numbers = []
self.a = 0
self.b = 1
def __call__(self,num):
for i in range(num):
self.numbers.append(self.a)
self.a, self.b = self.b, self.a+self.b
return self.numbers
class Fib(object):
def __init__(self):
self.numbers = []
self.a = 0
self.b = 1
def __call__(self,num):
for i in range(num):
self.numbers.append(self.a)
self.a, self.b = self.b, self.a+self.b
return self.numbers
2018-03-20
属性有类属性和实例对象属性,当定义一个类时就有了类属性,当创建一个实例对象时,会拷贝一份类属性给实例对象,即实例对象属性
类属性和实例对象属性都分为私有属性和公有属性
类的私有属性通过类方法调用
类的公有属性通过类方法调用,或者直接 类名.属性
实例对象的私有属性通过实例方法调用,
实例对象的公有属性通过实例方法调用,或者 实例名.属性
类属性和实例对象属性都分为私有属性和公有属性
类的私有属性通过类方法调用
类的公有属性通过类方法调用,或者直接 类名.属性
实例对象的私有属性通过实例方法调用,
实例对象的公有属性通过实例方法调用,或者 实例名.属性
2018-03-20
class Person(object):
address='count'
count=0
def __init__(self,name):
self.name=name
Person.count=Person.count+1
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print Person.count
address='count'
count=0
def __init__(self,name):
self.name=name
Person.count=Person.count+1
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print Person.count
2018-03-19
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'
我也很绝望啊,try一下
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'
我也很绝望啊,try一下
2018-03-19
class Person(object):
def __init__(self,name,gender,birth,job):
self.name=name
self.gender=gender
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,gender,birth,job):
self.name=name
self.gender=gender
self.birth=birth
self.job=job
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
print xiaoming.name
print xiaoming.job
本来我一点疑惑都没,看了答案和你们的答案我反而一脸懵。。
2018-03-19
python中is_integer()可精确判断整数、浮点数是否为整数
import math
def is_sqr(x):
return math.sqrt(x).is_integer()
print filter(is_sqr, range(1, 101))
import math
def is_sqr(x):
return math.sqrt(x).is_integer()
print filter(is_sqr, range(1, 101))
2018-03-19