-
class Animal ():
??? def __init__(self,name,age)
??? self.name=name
??? self.age=age
dog=Animal('dog',2)
cat=Animal('cat',6)
print(dog.name,dog.age)
print(cat.name,cat.age)查看全部 -
class Animals() :
dog=Animal()
cat=Animal()查看全部 -
1.類的私有屬性以__開頭,無法通過外部訪問,只能通過內部方法訪問,為了保護類或實例屬性不被外部污染而設計的。
查看全部 -
str = "Hello"
my_list = [str]
print(my_list)? # 輸出: ['Hello']
str = "Hello"
as_item_list = list(str)
print(as_item_list)? # 輸出: ['H', 'e', 'l', 'l', 'o']查看全部 -
4-4? Python類的數學運算
如何理解def __add__(self, r):
? ? ? ?return Rational(self.p * r.q + self.q * r.p, self.q * r.q)>>> r1 = Rational(1, 2)
>>> r2 = Rational(2, 3)
>>> print(r1 + r2)
思路過程:
不理解r是什么,后面經過查找,代碼存在r1+r2時才會調用__add__()函數,此時self.p和self.q指的是r1的p和q,而r.p和r.q指的是r2.p和r2.q
也就是 (1*3+2*2)/2*3 有理數的答案
查看全部 -
class?Rational(object): ????def?__init__(self,?p,?q): ????????self.p?=?p ????????self.q?=?q ????def?__add__(self,?r): ????????return?Rational(self.p?*?r.q?+?self.q?*?r.p,?self.q?*?r.q) ????def?__str__(self): ????????return?'{}/{}'.format(self.p,?self.q) ???????? ???? r1?=?Rational(1,?2) r2?=?Rational(2,?3) r3?=?Rational(1,?6) print(r1+r2) print(r1.__add__(r2))
重載運算符:
r1+r2 和
r1.__add__(r2) 相同
查看全部 -
class?Fib(object): ????def?__init__(self,num): ????????self.num=num ???? ????def?__len__(self): ????????return?len(self.num) f?=?[0,1,1,2,3,5,8,13,21,34] f_?=?Fib(f) print(f) print(len(Fib(f)))
Fib是一個類
f_ 是這個類的一個實例,等同于Fib(f)
f 是一個列表,也是f_ 的屬性
查看全部 -
私有屬性從類的內部進行訪問
第5行
class?Animal(object): ????def?__init__(self,name,age): ????????self.name=name ????????self.__age=age ????????print(self.__age) dog=Animal('D',5)
查看全部 -
1,實例屬性的優先級高于類屬性
2,通過實例可以訪問類屬性,但不能修改類屬性
3,類屬性私有化之后,不能從外部訪問,只能在類內部訪問
查看全部 -
class Animal(object):
??? __localtion = 'Asia'
??? __count = 0
??? def __init__(self, name, age):
??????? Animal.__count += 1
??????? self.name = name
??????? self.age = age
??? @classmethod
??? def set_localtion(cls, localtion):
??????? cls.__localtion = localtion
??? @classmethod
??? def get_localtion(cls):
??????? return cls.__localtion
??? @classmethod
??? def get_count(cls):
??????? return cls.__count
print(Animal.get_localtion()) # ==> Asia
Animal.set_localtion('Afica')
print(Animal.get_localtion()) # ==> Africa
print(Animal.get_count()) # ==> 0
cat = Animal('Huamao',2)
print(Animal.get_count()) # ==> 1
查看全部 -
class Animal(object):
??? def __init__(self, name, age, localtion):
??????? self.__name = name
??????? self.__age = age
??????? self.__localtion = localtion
??? def get_info(self):
??????? return 'name = {}, age = {}, localtion = {}'.format(self.__name, self.__age, self.__localtion)
??? def set_info(self,name,age,localtion):
??????? self.__name = name
??????? self.__age = age
??????? self.__localtion = localtion
dog = Animal('wangwang', 1, 'GuangDong)
print(dog.get_info())
dog.set_info('cat',2,'GuiYang')
print(dog.get_info())查看全部 -
class Animal(object):
??? localtion = 'Asia'
??? count = 0
??? def __init__(self, name, age):
??????? Animal.count += 1
??????? self.name = name
??????? self.age = age查看全部 -
在Python中初次操作文件可能會嘗試先讀file.white()后寫file.read(),結果會發現明明已經寫入數據,但讀出內容依然為空的問題。
如下例:
file = open('./a.txt', mode="a+", encoding="utf8")
file.write("123456")
str = file.read()
print(str)
# 讀出內容為空
# a.txt 內容為123456
1
2
3
4
5
6
造成這種問題的原因是,當寫入操作完成時,文件光標處于文件末尾處;接著繼續執行讀取操作,系統從光標處向后讀取,但光標已處于末尾,所以讀出內容為空。
該問題有兩種解決方案:
方法一:規范代碼,將讀取操作與寫入操作分類,在一種操作執行結束后應及時關閉文件指針file.close()。使用這種方法可以是文件安全有效,不會被其他操作影響預計效果。
如下:
# 寫入
file = open('./a.txt', mode="w", encoding="utf8")
file.write("123456")
file.close()
# 讀取
file = open('./a.txt', mode="r", encoding="utf8")
str = file.read()
print(str)
# 輸出為 123456
# a.txt 123456
1
2
3
4
5
6
7
8
9
10
方法二:使用seek()方法移動光標至指定位置
file.seek(offset,whence=0)
offset:偏移量,即需要移動偏移的字節數。
whence:要從哪個位置開始偏移,默認值為0;0代表從文件開頭開始算起,1代表從當前位置開始算起,2代表從文件末尾算起。
同時whence也可以使用常量名表示(需要引入os模塊):
whence常量:
os.SEEK_SET: 0 相對文件起始位置
os.SEEK_CUR: 1 相對文件的當前位置
os.SEEK_END: 2 相對文件的結束位置
對于本問題,可以在寫入之后將光標從新移動至文件開頭位置,然后執行讀取操作
file = open('./a.txt', mode="a+", encoding="utf8")
file.write("123456")
# 表示移動0個位置,從文件開頭開始
file.seek(0, 0)
str = file.read()
print(str)
# 輸出為 123456
# a.txt 123456
1
2
3
4
5
6
7
8
注意:Python3中不支持非二進制的文件光標移動位置且offset按位取值,所以非二進制文件建議只使用將光標移動到開頭或末尾的功能,即offset==0,避免出現io.UnsupportedOperation: can't do nonzero cur-relative seeks或者io.UnsupportedOperation: can't do nonzero end-relative seeks的錯誤
————————————————
? ? ? ? ? ? ? ? ? ? ? ? ? ? 版權聲明:本文為博主原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。
? ? ? ? ? ? ? ? ? ? ? ??
原文鏈接:https://blog.csdn.net/w18211679321/article/details/81451838
查看全部 -
# Enter a
def say_hello(name):
? ?print('您好呀,{}'.format(name))
? ?print(f"你好啊,{name}")
? ?print("寧浩呀,我是%s"%name)
say_hello("xioaming")查看全部 -
class?Person(object):
????def?__init__(self,?name,?gender):
????????self.name?=?name
????????self.gender?=?gender
?????????
class?Student(Person):????
????def?__init__(self,?name,?gender,?score):????????
????????super(Student,?self).__init__(name,?gender)????????
????????self.score?=?score???
?????????
????def?__str__(self):????????
????????return?'Student:?{},?{},?{}'.format(self.name,?self.gender,?self.score)????
????def?__repr__(self):????????
????????return?'STUDENT:?{},?{},?{}'.format(self.name,?self.gender,?self.score)
?????????
s?=?Student('Bob',?'Male',?88)
?
print(s)
print('%s'?%?s)
print('%r'?%?s)
print(str(s))
print(repr(s))
查看全部
舉報