3

I am looking at the Python cryptographic examples(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)

Next

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']

In my previous work, I have understood that without the argument dir()returns list of valid attributes.

Why do I have both '_d' and 'd'?

MikiBelavista
  • 2,470
  • 10
  • 48
  • 70
  • 2
    because `_d` and `d` are valid, underscore or not. `_d` is a naming convention discouraging you to use it, but it's still valid – Jean-François Fabre Aug 05 '18 at 09:43
  • 1
    Or in other words, the author of PyCriptodome named these two variables, and gave them similar names. The leading underscore is conventionally used to mark something as private; a common pattern is that if `v` needs an auxiliary internal object, its name would often be `_v`. – tripleee Aug 05 '18 at 09:46

1 Answers1

3

because _d and d are valid (and different from each other), underscore or not.

_d uses a naming convention discouraging you to use it from the outside of the class (with a possible exception from inherited classes), but it's still valid and visible.

On this simple example:

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

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

you get:

['_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']

note that you're getting

  • all "dunder methods" (spedial double underscore prefixed and suffixed methods used for special operations)
  • _bar as-is
  • bar as-is
  • even __foo but name mangled as _A__foo (like all non-dunder double underscore prefixed attributes)

also read What is the meaning of a single- and a double-underscore before an object name?

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219