Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python Advanced Objects Controlling Conversion

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 68,457 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!!

ohhh thank you i understand now