gpt4 book ai didi

python - 为什么下划线和 'normal' 变量都出现在 dir() 列表中?

转载 作者:太空宇宙 更新时间:2023-11-03 15:42:44 25 4
gpt4 key购买 nike

我在看 Python 加密示例 (PyCriptodome)

>>> from Crypto.Hash import SHA256
>>> from Crypto.PublicKey import RSA
>>> from Crypto import Random

>>> keyPair = RSA.generate(1024, rg)
>>> keyPair
RsaKey(n=136965403562203556008756172018307854596941050623319128208298141270753559641756880913579515252781318134605078590796965380573890909978973307484718815263206068396339040012530187738220917032617219191804241994374161791078493422872990621865765998883789114504025315394014914290915546672772138647618404548805335792391, e=65537, d=12633411119421403116910005948558386576109810504264219143677041426701027938941671805584451068908602287619019791588990276112259800583226172143142426999497696595164294852097497570448025127329063401092154745203714692627489061877805907641329178852991389621069959872155452247708311950473622509963157700897012362793, p=11087589806690192188252790347967677991722451309799179019881614471199138924752612698172266069109676252679178194446946306621193937032594190841131089738727723, q=12353036678860482962550037314677881356813045811345498557987093840147921697669238214163344188705969863296242048445184466718647041921862285219953085737805717, u=5930058794668004042843071106296071655835828106941133612195152599264918158827791715007378423398034795364513716281393195005242480751038879143818105780545780)

下一步

dir(keyPair)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_d', '_decrypt', '_e', '_encrypt', '_n', '_p', '_q', '_u', 'blind', 'can_encrypt', 'can_sign', 'd', 'decrypt', 'e', 'encrypt', 'exportKey', 'export_key', 'has_private', 'n', 'p', 'publickey', 'q', 'sign', 'size', 'size_in_bits', 'size_in_bytes', 'u', 'unblind', 'verify']

在我以前的工作中,我了解到没有参数 dir() 会返回有效属性列表。

为什么我同时有“_d”和“d”?

最佳答案

因为 _dd 是有效的(并且彼此不同),不管有没有下划线。

_d 使用命名约定,阻止您从类的外部使用它(继承类可能有异常(exception)),但它仍然有效且可见。

在这个简单的例子中:

class A:
def __foo(self):
pass
def _bar(self):
pass
def bar(self):
pass

a = A()
print(dir(a))

你得到:

['_A__foo', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_bar', 'bar']

注意你得到

  • 所有“双下划线方法”(特殊操作的双下划线前缀后缀方法)
  • _bar 原样
  • bar 原样
  • 甚至是 __fooname mangled_A__foo(就像所有非 dunder 双下划线前缀属性一样)

另请阅读 What is the meaning of a single- and a double-underscore before an object name?

关于python - 为什么下划线和 'normal' 变量都出现在 dir() 列表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51693161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com