-
只要重写了 __str__()方法,不管是打印对象还是打印对象的字符串格式,其调用的都是 __str__()方法,所以显示的都是重写的__str__()结果。也就是说,print(str(obj)) 和 print(obj) 是一样的。
而只有在控制台(window下的cmd,命令提示符)中,在python环境下直接输入(不是打印)obj,回车,调用的才是 __repr__()方法。如果没有重写该方法,还是会显示对象所在的地址。
查看全部 -
__count是类属性,如果将print(dog.__count)放到cat对象建立后面,将打印出2.因为cat对象建立后,Animal的属性__count就变了,dog能访问__count属性,所以就变了。
查看全部 -
# Enter a code
from functools import reduce
result = [item for item in map(lambda x:x*x,[1,2,3,4,5,6])]
print (result)
print reduce(lambda x,y:x+y,[1,3,5,7,9])
print sorted(['bob','MD','22A','QQ','mm'],key=lambda item:item.lower(),reverse = True)
查看全部 -
# Enter a code
L=[2,4,1,45,12,33,11,44,21]
K=['aad','']
print sorted(L)
def k(item):
return item.lower()
print sorted(['Bod','UUU1PPP','qqasd'],key=k ,reverse=True)
查看全部 -
2我为1wee
查看全部 -
# Enter a code
import math
def is_odd(x):
return x%2 ==1
for item in filter(is_odd,[1,6,9,54,52,2,4,8,7]):
print(item)
def is_empty(s):
return s and len(s.strip())>0
L =['te st',None,' str','',' ','END']
for item1 in filter (is_empty,L):
print(item1)
def sqr(x):
r = int(math.sqrt(x))
return r*r == x
for item2 in filter(sqr,(range(1,101))):
print item2
查看全部 -
from functools import reduce
def f(x,y):
return x*y
print(reduce(f,[1,3,5,7,9,12,4,5,100,7894,7896321,789456123]))
查看全部 -
# Enter a code
import math
def add(x,y,f):
return f(x)+f(y)
print(add(81,64,math.sqrt))
查看全部 -
python -m http.server 9999 -d C:/
查看全部 -
lass op(object):
def ok(num):
f = open('ok1.txt',mode='a') #读取中文
f.writelines('\n'+num)
f.close()
e = open('ok81.txt',mode='a+')
e.writelines(num+'\n')
e.close();
gl = open('ok81.txt',mode='r')
content = gl.readlines()
print(content)
gl.close()查看全部 -
class op(object):
def ok(num):
f = open('ok1.txt',mode='w') #读取中文
f.writelines(num)
f.close()查看全部 -
import os
class op(object):
def ok(num):
f = open('ok1.txt', encoding='utf-8') #读取中文
s = f.read(num)
l= f.readlines()
print(s)
print(l)
f.close()查看全部 -
线下已实现
#opfile.py
import os
class op(object):
def ok():
f = open('ok81.txt', 'x')
print ('ok92')
f.close()#main.py
import opfile
# This is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
print_hi('PyCharm')
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
num = input('pls input the num:'+'\n')
print('this is num is:{}'.format(num))
opfile.op.ok();查看全部 -
class person(object):
def __init__(self,name,gender):
self.name = name
self.gender = gender
def __call__(self,friend):
print('my name is {}...'.format(self.name))
print('my friend is {}...'.format(friend))
p= person('alice','female')
p('box')
查看全部 -
看不懂
查看全部 -
dog.name='xiaocui'
dog.age=7
cat.name='cuicui'
cat.age=8
print(dog.name)=xiaocui
print(dog.age)=7
print(cat.name)=cuicui
print(cat.age)=8
查看全部
举报