for example:
#create a complex data object
>>> complex_data=1+1j
#find out all attributes of complex_data using method dir()
>>> dir(complex_data)
['__abs__', '__add__', '__class__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__int__', '__le__',
'__long__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__nonzero__', '__pos__', '__pow__', '__radd__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__rpow__', '__rsub__',
'__rtruediv__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', 'conjugate', 'imag', 'real']
#access complex_data's attributes using ' . '
>>> complex_data.real
1.0
>>> complex_data.imag
1.0
>>> complex_data.conjugate()
(1-1j)
>>>
本文通过实例演示了如何创建Python中的复数对象,并利用dir()方法探索其所有属性。此外,还介绍了如何通过点操作符访问这些属性,包括实部、虚部及共轭等。

被折叠的 条评论
为什么被折叠?



