最新回答 / qq_BackatOne_1
Python语言中,在对象类里面,不用提前定义变量,而是在__init_初始化函数中,统一将所有的默认属性进行初始化。所以不需要生命,而是直接用!
2018-01-24
# -*- coding: utf-8 -*-
class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = score
def get_grade(self):
if self.__score >=90:
return 'A-优秀'
elif self.__score >=60:
return 'B-及格'
return 'C-不及格'
class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = score
def get_grade(self):
if self.__score >=90:
return 'A-优秀'
elif self.__score >=60:
return 'B-及格'
return 'C-不及格'
2018-01-24
class Person(object):
__count = 0
def __init__(self,name):
self.name = name
Person.__count += 1
@classmethod
def how_many(cls):
return cls.__count
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()
__count = 0
def __init__(self,name):
self.name = name
Person.__count += 1
@classmethod
def how_many(cls):
return cls.__count
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()
2018-01-24
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-24
try:
import json
except ImportError:
import simplejson as json
print json.dumps({'python':2.7})
import json
except ImportError:
import simplejson as json
print json.dumps({'python':2.7})
2018-01-24
最赞回答 / qq_PshINHYE_04165752
sorted()函数可以接受一个参数sorted()函数还可以接受一个key函数来实现自定义的排序。sorted()还可以接受第三个参数:reverse=True,来实现反序排列<...code...>iterable -- 可迭代对象。cmp -- 比较的...
2018-01-24
from os.path import isdir,isfile
print isdir(r'/data/webroot/resource/python')
print isfile(r'/data/webroot/resource/python/test.txt')
输出的都是False
print isdir(r'/data/webroot/resource/python')
print isfile(r'/data/webroot/resource/python/test.txt')
输出的都是False
2018-01-24
已采纳回答 / 云才哥
因为math.sqrt()函数会返回一个float类型(浮点型)数,所以要判断该数的平方根是否为整数要现将其转换为整型(float类型转换为整型会将其从小数点截断只保留整数)然后再判断该数的平方是否与x相等即可,相等即可证明x的平方根为整数。int(math.sqrt(x))中math.sqrt(x)表示求x的平方根,int()表示将其转换为整型return r*r==x表示判断r(经过类型转换得到的值)的平方是否等于x,等于则返回ture,不等于则返回false
2018-01-24
import time, functools
def performance(unit):
start = time.time()
def perf_decorator(f):
@functools.wraps(f)
def a(*args, **kw):
print '[%s] %s()...' % (unit,f.__name__),time.time()-start
return f(*args,**kw)
return a
return perf_decorator
def performance(unit):
start = time.time()
def perf_decorator(f):
@functools.wraps(f)
def a(*args, **kw):
print '[%s] %s()...' % (unit,f.__name__),time.time()-start
return f(*args,**kw)
return a
return perf_decorator
2018-01-23
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 p1.__count+p2.__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 p1.__count+p2.__count
except AttributeError:
print "attributeerror"
2018-01-23