Python 之 內建方法二 (Python 第十篇)
阿新 • • 發佈:2020-12-24
__dict
print('建立字串:好好學習,天天向上')
strtest = 'good good study,day day up'
# 顯示屬性
# 方法屬性和方法物件屬性不同
class person:
name = 'Yorlen'
age = 23
def __init__(self):
self.name = 'none'
self.age = 99
@staticmethod
def say():
print('Hello!' )
pass
print('列印類Person的dict: \t', person.__dict__)
'''
類的內建函式和自定義方法及屬性都放在類__dict__裡面
物件中的__dict__中儲存了一些初始化中的屬性及屬性值
但是 內建的資料型別 是沒有__dict__的
如果存在繼承關係,子父類有各自的__dict__
但是例項化之後,如果子類重寫,那麼__dict__都與子類一直
否則與父類一致。
'''
person1 = person()
print('列印person1的dict:\t', person1.__dict__)
dic = {'name': 'Yorlen' }
try:
print('列印字典dic的dict:', dic.__dict__)
except AttributeError as reason:
print('列印字典dic的dict錯誤原因:\t', reason)
# 返回當前範圍內的變數、方法和定義的型別列表
# 帶引數時,返回引數的屬性、方法列表。
print('__dir____dir____dir____dir__:' + str(strtest.__dir__()))
print('在上面結果中我們會看到每個欄位都是字串的內建方法。')
'''
The base class of the class hierarchy.
這個方法在類的分級中屬於基類
When called, it accepts no arguments and
當呼叫它時,它不接受引數
returns a new featureless instance that has
並返回一個新的無特性的例項
no instance attributes and cannot be given any.
該例項沒有例項屬性,也不能被賦予任何例項屬性。
'''
結果
建立字串:好好學習,天天向上
列印類Person的dict: {'__module__': '__main__',
'name': 'Yorlen', 'age': 23,
'__init__': <function person
.__init__ at 0x0000000001E23040>, 'say': <staticmethod object at
0x0000000001E07A00>, '__dict__': <attribute '__dict__'
of 'person' objects>, '__weakref__': <attribute '__weakref__' of
'person' objects>, '__doc__': None}
列印person1的dict: {'name': 'none', 'age': 99}
列印字典dic的dict錯誤原因: 'dict' object has no attribute '__dict__'
__dir____dir____dir____dir__:['__repr__', '__hash__', '__str__',
'__getattribute__', '__lt__', '__le__', '__eq__', '__ne__', '__gt__',
'__ge__', '__iter__', '__mod__', '__rmod__', '__len__', '__getitem__',
'__add__', '__mul__', '__rmul__', '__contains__', '__new__', 'encode',
'replace', 'split', 'rsplit', 'join', 'capitalize', 'casefold', 'title', 'center',
'count', 'expandtabs', 'find', 'partition', 'index', 'ljust', 'lower', 'lstrip',
'rfind', 'rindex',
'rjust', 'rstrip', 'rpartition', 'splitlines', 'strip', 'swapcase', 'translate',
'upper','startswith', 'endswith', 'isascii', 'islower', 'isupper', 'istitle',
'isspace', 'isdecimal', 'isdigit', 'isnumeric', 'isalpha', 'isalnum',
'isidentifier', 'isprintable', 'zfill', 'format', 'format_map', '__format__',
'maketrans', '__sizeof__', '__getnewargs__', '__doc__', '__setattr__',
'__delattr__', '__init__', '__reduce_ex__', '__reduce__',
'__subclasshook__', '__init_subclass__', '__dir__', '__class__']
在上面結果中我們會看到每個欄位都是字串的內建方法。
__eq
# eq = equals
# 作比較
strtest1 = 'good good study,day day up'
# 可變型別地址不同
l1 = [1, 2, 3, 4]
l2 = [1, 2, 3, 4]
print('字串和列表同類之間的比較')
print('id(l1), id(l2)', id(l1), id(l2))
print('l1.__eq__(l2)', l1.__eq__(l2))
print('l1 == l2', l1 == l2)
print('l1 is l2', l1 is l2)
# 不可變型別地址相同
print('id(strtest1), id(strtest)', id(strtest1), id(strtest))
print('strtest1.__eq__(strtest)', strtest1.__eq__(strtest))
print('strtest1 == strtest', strtest1 == strtest)
print('strtest1 is strtest', strtest1 is strtest)
結果
字串和列表同類之間的比較
id(l1), id(l2) 40636928 40623616
l1.__eq__(l2) True
l1 == l2 True
l1 is l2 False
id(strtest1), id(strtest) 31545264 31545264
strtest1.__eq__(strtest) True
strtest1 == strtest True
strtest1 is strtest True
__format
# format 格式化
# 當引數填空時,返回原字串或者說是原物件
print('strtest.__format__(\'\')', strtest.__format__(''))
class people:
def __init__(self, name, age):
self.name = name
self.age = age
# 重寫__format__方法
def __format__(self, format_spec):
if format_spec == '':
return str(self)
return "I'm " + self.name + ", " + str(self.age) + " years old!"
Jone = people('Jone', 18)
print('people.__format__(Jone, Jone)', people.__format__(Jone, Jone))
print('people.__format__(Jone, "")', people.__format__(Jone, ""))
print('Jone.__format__(Jone)', Jone.__format__(Jone))
print('Jone.__format__("")', Jone.__format__(""))
# self引數必須要填實參,如果是類名,會報警告
# 告訴你型別錯誤,請注意引數型別
# 這時候你就需要看看引數的引數有沒有對了
# print(people.__format__(people,""))
# 這句話也可以執行,但是警告無參
print('people.__format__(people(\'Rose\', 16), " "', people.__format__(people('Rose', 16), " "))
# 這裡1只是用作佔位符,讀者在第二個引數中寫任何內容都可以
print('people.__format__(Jone, 1)', people.__format__(Jone, 1))
結果
strtest.__format__('') good good study,day day up
people.__format__(Jone, Jone) I'm Jone, 18 years old!
people.__format__(Jone, "") <__main__.people object at 0x0000000001E1A040>
Jone.__format__(Jone) I'm Jone, 18 years old!
Jone.__format__("") <__main__.people object at 0x0000000001E1A040>
people.__format__(people('Rose', 16), " " I'm Rose, 16 years old!
people.__format__(Jone, 1) I'm Jone, 18 years old!
轉發評論收藏加關注呦
轉發評論收藏加關注呦
轉發評論收藏加關注呦
轉發評論收藏加關注呦
轉發評論收藏加關注呦