1

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

balderman
  • 22,927
  • 7
  • 34
  • 52
Andres Mitre
  • 641
  • 1
  • 11
  • 23
  • In your code you missed: 1. the constructor of the class 2. creating an instance of the class 3. calling `birth()`. – Klaus D. Nov 06 '20 at 07:09

4 Answers4

3

Yeah, that's the memory address. You can also obtain it with the id() built-in function.

Jiří Baum
  • 6,697
  • 2
  • 17
  • 17
  • Oh thanks for sharing @sabik . I tested it and it returns the memory address in base-10. I couldn't find it out there. Thank you. – Andres Mitre Nov 06 '20 at 07:10
  • @AndresMitre - You can convert the address from the `id()` function to hex using either the `hex()` function or one of the integer formatting options (`%x` or `{:x}`). – Jiří Baum Nov 07 '20 at 09:10
3

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.)
Wasif
  • 14,755
  • 3
  • 14
  • 34
1

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()
balderman
  • 22,927
  • 7
  • 34
  • 52
  • oh maybe I was not clear @balderman. I was just wondering what was the hex number about. It turns out that it is the function id. Thanks for answering anyways – Andres Mitre Nov 06 '20 at 07:12
  • @AndresMitre I was under the impression that by the end you would like to activate `birth` so I have posted my answer :-) – balderman Nov 06 '20 at 07:14
1
  • 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>
Federico Baù
  • 6,013
  • 5
  • 30
  • 38
  • Thanks for answering. Most completed answer yet. I think **id()** function might be useful in hardware or mobile applications when we are really concern about the memory, but those type of applications usually requires other languages (HDL). I really don't see when this might be useful. Have you ever used that function? – Andres Mitre Nov 06 '20 at 21:23
  • @AndresMitre - `id()` can be useful when you need a unique identifier of an object for some reason, for example to keep track of whether you've already seen it while processing some complex data structure. – Jiří Baum Nov 07 '20 at 09:13
  • @andres there for sure some use in a real qord application. At the moment i only used it for debugging purposes or understand if an object is still the same after have processed. Is actually just like use the is operator ( print(a is b)) for instance function itertools.repeat(n, i) it creates an infinite 'copies' of obj i by times of n. However are really the same object. When i first used it i checked it by printing the id if each item. Finding out is the same object. – Federico Baù Nov 07 '20 at 09:49
  • Actually here an SOO that shows a used scenario of the id function https://stackoverflow.com/a/29087011/13903942 – Federico Baù Nov 07 '20 at 09:55