Python Object-Oriented Python Advanced Objects Controlling Conversion

Blue DiamondXD
Blue DiamondXD
2,345 Points

i dont get it, is __int__, __float__, __init__, and__name__ names or variavles name or they have they own use??

;-;

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 56,648 Points

Variable names with a double-underscore prefix and suffix are typically reserved for built-in Python object attributes and methods and each has a special purpose.

As mentioned in PEP 8:

__double_leading_and_trailing_underscore__: "magic" objects or attributes that live in user-controlled namespaces. E.g. __init__, __import__ or __file__. Never invent such names; only use them as documented.

As for your list:

  • __int__: name of class method called when class instance issued in an integer context
  • __float__: name of class method called when class instance issued in an float context
  • __init__: name of class method called to initialize a class instance after it has been created
  • __name__: an object attribute containing the string name of the object. see below
>>> def foo():
...     pass
... 
>>> foo
<function foo at 0x7ffa6241ba60>
>>> f = foo
>>> f
<function foo at 0x7ffa6241ba60>
>>> f.__name__
'foo'

Post back if you need more help. Good luck!!

Blue DiamondXD
Blue DiamondXD
2,345 Points

ohhh thank you i understand now