vars() 函數返回給定對象的 __dict__ 屬性。
用法:
vars(object)
參數:
vars()
最多接受一個參數。
- object- 可以是模塊、類、實例或任何具有
__dict__
屬性。
返回:
vars()
返回給定對象的__dict__
屬性。- 如果傳遞給
vars()
的對象沒有__dict__
屬性,則會引發TypeError
異常。 - 如果沒有參數傳遞給
vars()
,則此函數的行為類似於 locals() function 。
注意: __dict__
是字典或映射對象。它存儲對象的(可寫)屬性。
示例:Python vars() 的工作
class Foo:
def __init__(self, a = 5, b = 10):
self.a = a
self.b = b
object = Foo()
print(vars(object))
輸出
{'a': 5, 'b': 10}
此外,在 Python shell 上運行這些語句:
>>> vars(list)
>>> vars(str)
>>> vars(dict)
相關用法
- Python vars()用法及代碼示例
- Python statistics variance()用法及代碼示例
- Python torch.distributed.rpc.rpc_async用法及代碼示例
- Python torch.nn.InstanceNorm3d用法及代碼示例
- Python pandas.arrays.IntervalArray.is_empty用法及代碼示例
- Python tf.compat.v1.distributions.Multinomial.stddev用法及代碼示例
- Python numpy.less()用法及代碼示例
- Python tf.compat.v1.distribute.MirroredStrategy.experimental_distribute_dataset用法及代碼示例
- Python Sympy Permutation.list()用法及代碼示例
- Python scipy.ndimage.binary_opening用法及代碼示例
- Python pyspark.pandas.Series.dropna用法及代碼示例
- Python torchaudio.transforms.Fade用法及代碼示例
- Python pyspark.pandas.groupby.SeriesGroupBy.unique用法及代碼示例
- Python numpy.polynomial.hermite.hermmul用法及代碼示例
- Python tf.compat.v1.data.TFRecordDataset.interleave用法及代碼示例
- Python tf.summary.scalar用法及代碼示例
- Python scipy.signal.windows.tukey用法及代碼示例
- Python numpy.seterrobj用法及代碼示例
- Python Matplotlib.figure.Figure.subplots_adjust()用法及代碼示例
- Python tf.linalg.LinearOperatorFullMatrix.matvec用法及代碼示例
注:本文由純淨天空篩選整理自 Python vars()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。