Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python, what does '<function at ...>' mean?

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

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

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

like image 516
JadedTuna Avatar asked Oct 12 '13 11:10

JadedTuna


People also ask

What does * mean in Python function?

The asterisk (star) operator is used in Python with more than one meaning attached to it. For numeric data types, * is used as multiplication operator >>> a=10;b=20 >>> a*b 200 >>> a=1.5; b=2.5; >>> a*b 3.75 >>> a=2+3j; b=3+2j >>> a*b 13j.

What does -> in Python function mean?

The -> (arrow) is used to annotate the return value for a function in Python 3.0 or later.

What does -> int mean in Python?

The int() function converts the specified value into an integer number.

What does object at mean in Python?

This is the definition: An object is created using the constructor of the class. This object will then be called the instance of the class. Wikipedia defines it like this: An object is an instance of a Class. Objects are an abstraction. They hold both data, and ways to manipulate the data.


4 Answers

You are looking at the default representation of a function object. It provides you with a name and a unique id, which in CPython happens to be a memory address.

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

In other words, if you have two function objects which were originally named main, you can still see that they are different:

>>> def main(): pass
... 
>>> foo = main
>>> def main(): pass
... 
>>> foo is main
False
>>> foo
<function main at 0x1004ca500>
>>> main
<function main at 0x1005778c0>
like image 111
Martijn Pieters Avatar answered Oct 01 '22 09:10

Martijn Pieters


It's function's identity, in CPython implementation it's the address of the object in memory.

like image 25
aga Avatar answered Oct 01 '22 08:10

aga


Every object obj has a method obj.__repr__( )
When called, this method returns a string object that is the "official" printable representation of the object obj

When the Python interpreter encounters
a line print obj in a script
or >>> obj in a command line,
the method __repr__( ) of the object is called and the value of the representative string returned is displayed on the screen.

The __repr__( ) method of an object can specifically be called by using the built-in function repr( ) with the object's name as argument, to assign the string returned by __repr__( ) to an identifier, so being able to perform operations on this representation.
Only in Python 1 and Python 2, reversed quotes around the name of an object have the same effect than calling repr( ) on it.

Compare:

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

result

__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'>

.

In <function main at 0x00FB2930>, the part 0x00FB2930 represents the memory address of the object (here a function), that is to say an integer that references the location of the object in the RAM.

0x00FB2930 is an hexinteger, that is to say a literal representing the value of the memory address in base 16.

This memory address is precisely returned by the built-in function id(), whose value is printed as a decimalinteger literal, that is to 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'>
like image 41
eyquem Avatar answered Oct 01 '22 08:10

eyquem


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

like image 39
Games Brainiac Avatar answered Oct 01 '22 08:10

Games Brainiac