2

I am a beginning python programmer and trying to learn the nuances in the language. I was trying out the types of objects that can be stored inside the values key in a dict structure. Here goes my sample code:

  myMap = {'a':1,'b':hash}
  def myFun (a,b):
      return a + b
  myMap['c']=myFun
  myMap

And here is the output:

{'a': 1, 'b': <function hash(obj, /)>, 'c': <function __main__.myFun(a, b)>}

I want to know why the function reference is enclosed by angle brackets. I have an inkling that this may be due to some resemblance to C code, but I am not able to get a definite answer. I read somewhere that this indicates that a function is a “special” object. But a more elaborate answer would be very helpful. Thanks for any suggestions.

sophros
  • 14,672
  • 11
  • 46
  • 75
Soumdtt
  • 25
  • 2

1 Answers1

1

The angle brackets are typically used to denote part of the output that is related not to values themselves but rather to metadata related to the values.

In your case - there is no good way to represent a function in Python without providing its definition. However, <function __main__.myFun(a, b)> is a good way to print information referring to the function by providing its name and parameters.

There is no relation to C code other than maybe some convention but I am not aware of any.

Printing in the console of the representation of values of more complex objects in Python is handled by special ("magic") function __repr__ or __str__ depending on the situation.

sophros
  • 14,672
  • 11
  • 46
  • 75