简体   繁体   English

在python中打印方法时十六进制值是什么意思?

[英]What does the hex value means when printing a method in python?

just a simple and quick question that I cannot find the answer.只是一个简单而快速的问题,我找不到答案。 I have this class with this method inside:我有这个类,里面有这个方法:

class Pokemon:
    "This is a pokemon class"
     number = 10

    def birth(self):
        print('Hello')

print(Pokemon.birth)

Output:输出:

<function Pokemon.birth at 0x7fc78c6e8160>

When I print the method it returns a hex number, what does this hex exactly mean?当我打印它返回一个十六进制数的方法时,这个十六进制到底是什么意思? memory location?内存位置? I am trying to understand the process behind the code.我试图了解代码背后的过程。

PD. PD。 I am not trying to make the class work, just curiosity about the hex我不是想让这门课起作用,只是对十六进制好奇

Thanks谢谢

Yeah, that's the memory address.是的,那是内存地址。 You can also obtain it with the id() built-in function .您也可以使用id()内置函数获取它。

This is the memory address of the function.这是函数的内存地址。

You need to use parentheses when calling a function.调用函数时需要使用括号。 Interpreter won't complain/crash if you don't use parentheses to call the function, it will just tell you that what you have is a function handle (ie the message that you got, memory address).如果你不使用括号来调用函数,解释器不会抱怨/崩溃,它只会告诉你你拥有的是一个函数句柄(即你得到的消息,内存地址)。

BTW you can get hex memory address of a function using id()顺便说一句,您可以使用id()获取函数的十六进制内存地址

Help on built-in function id in module builtins: id(obj, /) Return the identity of an object. This is guaranteed to be unique among simultaneously existing objects. (CPython uses the object's memory address.)

If you want to invoke a function you should invoke it by calling birth() .如果你想调用一个函数,你应该通过调用birth()来调用它。 Your call just show the identity of birth你的电话只显示birth的身份

In order to make your code work you need to create an instance of your class.为了使您的代码工作,您需要创建一个类的实例。

pokemon is an instance of the class Pokemon so you can call birth which is instance method. pokemonPokemon类的一个实例,因此您可以调用属于实例方法的birth See here for more about this topic.有关此主题的更多信息,请参见此处

class Pokemon:
    "This is a pokemon class"
    number = 10

    def birth(self):
        print('Hello')

pokemon = Pokemon()
pokemon.birth()
  • what does this hex exactly mean?这个十六进制到底是什么意思? memory location?内存位置?

Yes is the address of the object in memory. Yes 是对象在内存中的地址。 it is represented in hexadecimal number in order to make it more 'human readable.'它以十六进制数表示,以使其更具“人类可读性”。

import sys

class Pokemon:
    "This is a Pokemon class"
    numner = 10

    def birth(self):
        print('Hello')


print(Pokemon)
#<class '__main__.Pokemon'>

print(Pokemon.birth)
#<function Pokemon.birth at 0x000001F83C177C10>

print(hex(id(Pokemon)))
#0x1f83bf6fad0

print(hex(id(Pokemon.birth)))
#0x1f83c177c10

print(Pokemon.birth.__repr__)
# <method-wrapper '__repr__' of function object at 0x0000018609D87C10>

Interesting features有趣的功能

Only the memory consumption directly attributed to the object is accounted for, not the memory consumption of objects it refers to.只考虑直接归因于对象的内存消耗,而不是它所引用的对象的内存消耗。

print(sys.getsizeof(Pokemon))
print(sys.getsizeof(Pokemon.birth))
#1064
#136
  • Reference Counts and Namespace引用计数和命名空间

The code below show where the object Pokemon.birth leaves.下面的代码显示了对象 Pokemon.birth 离开的地方。 It is not in the globals() namespace but rather within the '__main.__Pokemon' class namespace.它不在 globals() 命名空间中,而是在 '__main.__Pokemon' 类命名空间中。

print(globals()['Pokemon'])
# <class '__main__.Pokemon'>
print(Pokemon.__dict__['birth'])
# <function Pokemon.birth at 0x0000020D51167C10>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM