简体   繁体   中英

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. 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 .

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()

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() . Your call just show the identity of 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. 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. 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. It is not in the globals() namespace but rather within the '__main.__Pokemon' class namespace.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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