Python 涓?Type 鍜?Object
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 涓?Type 鍜?Object相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/tar' title='tar'>tar
杩斿洖 method key 闃呰 鍖呮嫭 鍑芥暟 鍒涘缓 imp 鍐欒繖绡囧崥鏂囨椂鍗佸垎蹇愬繎锝?涓旇皥涓€涓嬫垜鐨勮璇嗭紝鏈夐敊鐨勬杩庣暀瑷€鎸囨銆?/p>
鏄庣‘鍑犵偣
Python涓竴鍒囩殕瀵硅薄
鎵€鏈夌殑绫?閮界户鎵胯嚜 object
锛屼篃灏辨槸璇?object
鏄墍鏈夌被鐨勫熀绫伙紙瓒呯被锛?br>
type
涔熺户鎵胯嚜 object
婧愮爜涓璽ype 鐨勫畾涔夛細class type(object):
type
鏄? objcet
鐨勭被鍨?鍚屾椂 object
鏄? type
鐨勮秴绫?br>
鏄庣‘缁ф壙鍏锋湁浼犻€掓€с€?code> 楦?/code> 缁ф壙浜?code> 瀹剁
锛?code> 瀹剁
鍙堢户鎵夸簡 绂界被
锛屽洜姝ら浮涔熷睘浜庣绫伙紝灏辨槸杩欐牱鐨勫叧绯汇€?br>
绫诲拰瀹炰緥鍏崇郴銆傜被涔熸槸瀵硅薄锛屽湪涓や釜瀵硅薄涓紝涓€涓槸鍙︿竴涓殑瀹炰緥銆備緥濡傞浮鏄竴涓被 鍙皬绾㈢殑楦℃槸楦$殑瀹炰緥銆?br>
鍦≒ython3涓紝绫诲拰绫诲瀷宸茬粡鏄竴绉嶄笢瑗夸簡
銆?br>
鍦≒ython3 涓彧鏈夌被鍜岄潪绫讳袱绉嶅璞°€傜被鍨嬫槸"<class 鈥榯ype鈥?gt;" 閭d箞瀹冩槸绫诲惁鍒欏畠鏄潪绫汇€傚惉璧锋潵寰堢粫锛岃嫳鏂囧師鏂囷細There are only two kinds of objects in Python: to be unambiguous let鈥榮 call these types and non-types. Non-types could be called instances, but that term could also refer to a type, since a type is always an instance of another type. Types could also be called classes, and I do call them classes from time to time.
class Chicken(object): # 楦$被 浠栦笉浠呬粎鏄被 涔熸槸涓€涓璞★紝瀹冩槸 type 鐨勫璞? pass
xiaohong = Chicken() # 瀹冩槸楦$被 杩欎釜绫荤殑瀵硅薄
Python涓€鍒囩殕瀵硅薄
1 鍦≒ython涓?鎴戜滑甯歌鐨? 绫?/code> 涔熸槸 瀵硅薄
锛岃€屽畠浠殑绫绘槸 鍏冪被 : type
绠€鍗曠偣鏄撲簬鐞嗚В绉颁负 绫荤殑绫?/code>
2 鍐呭缓鐨勭被(绫诲瀷)锛宱bject銆乴ist銆乼uple銆乨ict銆?/p>
3 鑷畾涔夌殑绫?MyClass 绛夌瓑浠讳綍浣犲枩娆㈢殑鍚嶅瓧銆?/p>
4 鐢? 鑷畾涔夌殑绫?/code> 鍜? 鍐呭缓鐨勭被
瀹炰緥鍖栫殑瀵硅薄灏辨槸鎴戜滑甯歌鐨? 瀵硅薄
浜嗭紝涔熷彨瀵硅薄鐨勫疄渚嬨€?/p>
涓ょ鍏崇郴
鍦ㄩ潰鍚戝璞$殑涓栫晫閲屾湁涓ょ鍏崇郴锛岃繖涓庤瑷€鏈韩娌℃湁鍏崇郴锛屽彧瑕佹槸闈㈠悜瀵硅薄鐨勮瑷€閮芥嫢鏈夎繖鏍风殑涓ょ灞炴€с€傜被鍨嬪疄渚嬪叧绯诲拰缁ф壙鍏崇郴銆?/p>
绫诲瀷瀹炰緥鍏崇郴
鍗宠绫荤敱璋佸垱寤?/p>
str(鈥榓bc鈥? 绫?str 鍒涘缓浜嗗疄渚?abc銆?
class Person(object):
""" A simple Person class. """
monkey = Person()
# monkey 鍜? Person 灏辨槸 绫诲瀷鍜屽疄渚嬬殑鍏崇郴 type(monkey) --> <class 鈥榑_main__.Person鈥?gt;
缁ф壙鍏崇郴
鍗崇被缁ф壙鑷皝
class MyList(list):
""" A custom list class , it鈥榮 a subclass of `list`. """
# MyList 灏辨槸 list 鐨勫瓙绫伙紝 瀹冨拰list 灏辨槸 缁ф壙鍏崇郴
# 杩欐槸Python 婧愮爜涓type 鐨勫畾涔?class type(object):
"""
type(object_or_name, bases, dict)
type(object) -> the object鈥榮 type
type(name, bases, dict) -> a new type
"""
...
# 鐢辨 鍙 type 鏄?object 鐨勫瓙绫?涔熷氨鏄?缁ф壙鍏崇郴 <1> type 缁ф壙鑷?object
>>> type(object)
<class 鈥榯ype鈥?gt;
>>> type(type)
<class 鈥榯ype鈥?gt;
# 涓婇潰涓よ浠g爜鐨勮緭鍑哄緢娓呮櫚 type 鍜?object 浠ュ強 type 涓?type 瀛樺湪 绫诲瀷瀹炰緥鍏崇郴
# <2> type 鏄?object 鐨勭被鍨?# <3> type 鏄?type 鐨勭被鍨?
鍒涘缓绫荤殑鏂规硶
class MyClass(object):
"""This is a simple class for display how to create a class. """
name = 鈥楴ewClass鈥?
def func(self):
print(鈥楬ello class, my name is %s鈥?% self.name)
new_obj = MyClass()
print(new_obj.func)
# <bound method MyClass.func of <__main__.MyClass object at 0x103b3b2e8>>
# Hello class, my name is NewClass
type鍑芥暟
type
杩欎釜鍑芥暟鏈夌偣鍌诲睂锛屽畠渚濇嵁鍙傛暟鐨勪笉鍚屾嫢鏈変袱绉嶅畬鍏ㄤ笉鍚岀殑琛屼负锛屽湪绋嬪簭璁捐涓槸寰堝彟绫荤殑锛屼技涔庝篃涓嶇鍚圥ython鐨勮璁″摬瀛︺€傛寜鐓ython鐨?Style 浠栧簲璇ユ槸涓や釜鍑芥暟鎵嶅锛屼絾鏄繖鏄负浜嗘洿濂界殑鍚戝悗鍏煎銆?/p>
type
鎺ュ彈涓変釜鍙傛暟 绗竴涓弬鏁颁负绫荤殑鍚嶅瓧锛?绗簩涓弬鏁颁负缁ф壙鍒楄〃锛?绗笁涓弬鏁颁负灞炴€у瓧鍏?灞炴€у拰鏂规硶)
def func(self):
""" A simple function to bound NewClass. """
print(鈥楬ello type, my name is %s鈥?% self.name)
TypeClass = type(鈥楾ypeClass鈥? (object,), {鈥榥ame鈥? 鈥楴ewClass鈥? 鈥榝unc鈥? func})
type_obj = TypeClass()
print(type_obj.func)
type_obj.func()
# <bound method func of <__main__.NewClass object at 0x100d2f128>>
# Hello type, my name is TypeClass
_call_
瀵硅薄閫氳繃鎻愪緵 __call(slef, *args ,**kwargs)__
鏂规硶鍙互妯℃嫙鍑芥暟鐨勮涓恒€傚嵆鍏佽浠ョ被/瀵硅薄() 鐨勫舰寮忔潵璋冪敤銆?/p>
鍏冪被鐨?code>__call__
鏂规硶鍦ㄧ户鎵跨殑绫昏繘琛屽疄渚嬪寲鏃惰皟鐢紙姝ゆ椂瀹炰緥鍖栧嚭鐨勬槸涓被锛岃€屼笉鏄璞★級
_new_
灏嗚嚜韬疄渚嬪寲鏃讹紝涔熷氨鏄垱寤哄璞℃椂璋冪敤鐨勬柟娉曪紝璇ユ柟娉曟槸涓€涓潤鎬佹柟娉曪紝绗竴涓弬鏁颁负绫绘湰韬紝杩斿洖鍊间负涓鸿瀵硅薄鍒嗛厤鐨勭┖闂淬€備篃灏辨槸琚垱寤虹殑杩欎釜瀵硅薄銆?code>__new__
鏂规硶鑷冲皯闇€瑕佷竴涓猚ls 鍙傛暟锛岃〃绀虹殑鏄皢瑕佽瀹炰緥鍖栫殑绫绘湰韬紝闇€瑕佹敞鎰忕殑鏄鏂规硶蹇呴』杩斿洖涓€涓┖闂达紝涔熷氨鏄繑鍥炲疄渚嬪寲鍚庣殑瀵硅薄 return super().\__new__()
鎴栬€?object.__new__()
_init_
鎷垮埌__new__
鏂规硶杩斿洖鐨勫璞★紝瀵硅繖涓柊鍒涘缓鍑烘潵鐨勫璞¤繘琛屼竴浜涘垵濮嬪寲鎿嶄綔銆?/p>
鏀瑰啓绫绘潵鎺у埗鍒涘缓鐨勫璞?/h3>
瀹炵幇涓€涓厤缃被锛岃绫婚渶瑕佷繚璇佸叏灞€鍙湁涓€涓疄渚嬶紝骞朵笖闇€瑕佷繚璇佸悓涓€鏃堕棿 閰嶇疆鏄竴鑷寸殑銆備篃灏辨槸闇€瑕佷繚璇佺嚎绋嬪畨鍏ㄣ€備笅闈㈡槸浠g爜锛岄€氳繃鎺у埗 __new__
鏂规硶鏉ュ疄鐜板崟渚嬨€?/p>
import threading
def synchronized(bar):
""" threading lock for Config"""
bar.__lock__ = threading.Lock() # get lock
def lock_func(*args, **kwargs):
with bar.__lock__:
return bar(*args, **kwargs)
return lock_func
class Config(object):
""" Singleton `Config` ."""
_instance = None
@synchronized
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = object.__new__(cls, *args, **kwargs)
return cls._instance
灏嗗嚱鏁颁綔涓哄厓绫?/h4>
鍋囪涓婁竴姝ョ殑鎿嶄綔涓渶瑕佸皢閰嶇疆绫讳腑鎵€鏈夌殑灞炴€ч兘鍙樹负澶у啓鐨勩€備互 _
寮€澶寸殑灞炴€ч櫎澶栵紝瑕佹€庝箞鍋氬憿锛?/p>
import threading
def synchronized(bar):
""" threading lock for Config"""
bar.__lock__ = threading.Lock() # get lock
def lock_func(*args, **kwargs):
with bar.__lock__:
return bar(*args, **kwargs)
return lock_func
def filter_config(cls_name, cls_mro, cls_attr):
cls_attr = {k.upper(): v for k, v in cls_attr.items() if not k.startswith(鈥榑鈥?}
return type(cls_name, cls_mro, cls_attr) # 杩斿洖涓€涓被
class Config(metaclass=filter_config): # 鎸囧畾鍏冪被
""" Singleton `Config` ."""
_instance = None
@synchronized
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = object.__new__(cls, *args, **kwargs)
return cls._instance
number = 100
config = Config()
print(config.NUMBER) # 100
灏嗙被浣滀负鍏冪被
import threading
def synchronized(bar):
""" threading lock for Config"""
bar.__lock__ = threading.Lock() # get lock
def lock_func(*args, **kwargs):
with bar.__lock__:
return bar(*args, **kwargs)
return lock_func
class Filter_config(type):
def __new__(cls, cls_name, cls_mro, cls_attr):
cls_attr = {k.upper(): v for k, v in cls_attr.items() if not k.startswith(鈥榑鈥?}
return super(Filter_config, cls).__new__(cls, cls_name, cls_mro, cls_attr)
class Config(metaclass=Filter_config): # 鎸囧畾鍏冪被
""" Singleton `Config` ."""
_instance = None
@synchronized
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = object.__new__(cls, *args, **kwargs)
return cls._instance
number = 100
config = Config()
print(config.NUMBER) # 100
以上是关于Python 涓?Type 鍜?Object的主要内容,如果未能解决你的问题,请参考以下文章