" mean? What does it mean ? Example: >>> def main(): ... pass ... >>> main

In Python, what does "<function in ...>" mean?

What does it mean <function at 'somewhere'>? Example:

>>> def main():
...     pass
...
>>> main
<function main at 0x7f95cf42f320>

And maybe there is a way to somehow access it using ? 0x7f95cf42f320

+4
source share
4 answers

You are viewing the default representation of a function object. It provides you with a name and a unique identifier, which in CPython is the memory address.

You cannot access it using the address; The memory address is used only to help you distinguish between functional objects.

, , main, , :

>>> def main(): pass
... 
>>> foo = main
>>> def main(): pass
... 
>>> foo is main
False
>>> foo
<function main at 0x1004ca500>
>>> main
<function main at 0x1005778c0>
+9

identity, CPython .

+3

obj obj.__ repr __()
, "" obj

Python print obj script
>>> obj ,
__ repr __() .

__ repr __() repr() , , __ repr __(), , .
Python 1 Python 2 , repr() .

:

def main():
    pass

if  '__repr__' in  dir(main):
    print ('__repr__ is a method  of main\n')
else:
     print ('main has no method __repr__\n')

print ('main : %s\n'
       'type(main) == %s\n'
       % (main,type(main)) )

print ('repr(main) : %s\n'
       'type(repr(main)) == %s'
       %(repr(main),type(repr(main))) )

# Only in Python 1 and Python 2, string conversions with
# reversed quotes produce the same result as repr():
print ('\n`main` : %s\n'
       'type(`main`) == %s'
       % (`main`,type(`main`)) )

__repr__ is a method  of main

main : <function main at 0x00FB2930>
type(main) == <type 'function'>

repr(main) : <function main at 0x00FB2930>
type(repr(main)) == <type 'str'>

.

<function main at 0x00FB2930> 0x00FB2930 ( ), , .

0x00FB2930 hexinteger, , 16.

This memory address is exactly returned by the built-in function id(), the value of which is printed as decimalinteger , that is, let's say its representation in base 10.

print ('repr(main) : %s\n'
       'type(repr(main)) == %s\n'
       % (repr(main),
          type(repr(main))) )

hex_address = repr(main)[18:-1]
print ('hex_address = repr(main)[18:-1]  defined\n'
       'hex_address : %s\n'
       'type(hex_address) == %s\n'
       'int(hex_address , 16) : %s\n'
       'type(int(hex_address , 16)) : %s\n'
       % (hex_address,
          type(hex_address),
          int(hex_address , 16),
          type(int(hex_address , 16))) )

print ('id(main) : %s\n'
       'type(id(main)) == %s\n'
       'hex(id(main) : %s\n'
       'type(hex(id(main)) : %s'
       % (id(main),
          type(id(main)),
          hex(id(main)),
          type(hex(id(main)))) )

result

repr(main) : <function main at 0x00FB2930>
type(repr(main)) == <type 'str'>

hex_address = repr(main)[18:-1]  defined
hex_address : 0x00FB2930
type(hex_address) == <type 'str'>
int(hex_address , 16) : 16460080
type(int(hex_address , 16)) : <type 'int'>

id(main) : 16460080
type(id(main)) == <type 'int'>
hex(id(main) : 0xfb2930
type(hex(id(main)) : <type 'str'>
+2
source

In CPython, this is simply the address of the object in memory. All objects have this, not just functions.

+1
source

All Articles