Reputation: 701
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
Upvotes: 1
Views: 962
Reputation: 7715
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>
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
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>
Upvotes: 1
Reputation: 23825
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()
Upvotes: 1
Reputation: 15498
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 (i.e. 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.)
Upvotes: 3
Reputation: 6940
Yeah, that's the memory address. You can also obtain it with the id()
built-in function.
Upvotes: 3