-
只要重寫了 __str__()方法,不管是打印對象還是打印對象的字符串格式,其調(diào)用的都是 __str__()方法,所以顯示的都是重寫的__str__()結(jié)果。也就是說,print(str(obj)) 和 print(obj) 是一樣的。
而只有在控制臺(window下的cmd,命令提示符)中,在python環(huán)境下直接輸入(不是打印)obj,回車,調(diào)用的才是 __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()查看全部 -
線下已實現(xiàn)
#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
查看全部
舉報