[Python] Why is cls.__dict__[meth] different than getattr(cls, meth) for...

Discussão em 'Python' iniciado por Stack, Maio 16, 2025.

  1. Stack

    Stack Membro Participativo

    I've never seen anything else work like this before.

    Is there anything else that does this?

    >>> class NothingSpecial:
    @classmethod
    def meth(cls): pass

    >>> NothingSpecial.meth
    <bound method classobj.meth of <class __main__.NothingSpecial at 0x02C68C70>>
    >>> NothingSpecial.__dict__['meth']
    <classmethod object at 0x03F15FD0>

    >>> getattr(NothingSpecial, 'meth')
    <bound method NothingSpecial.meth of <class '__main__.NothingSpecial'>>

    >>> object.__getattribute__(NothingSpecial, 'meth')
    <classmethod object at 0x03FAFE90>

    >>> type.__getattribute__(NothingSpecial, 'meth')
    <bound method NothingSpecial.meth of <class '__main__.NothingSpecial'>>

    Continue reading...

Compartilhe esta Página