6

We know that everything is an object in Python and so that includes integers. So doing dir(34) is no surprise, there are attributes available.

My confusion stems from the following, why is it that doing 34.__class__ gives a syntax error when I know that 34 does have the attribute __class__. Furthermore, why does binding an integer to a name, say x, and then doing x.__class__ yield my expected answer of type int?

1 Answers1

12

Because 34.__class__ is not a valid floating-point number, which is what the . denotes in a numeric literal. Try (34).__class__.

kindall
  • 178,883
  • 35
  • 278
  • 309
  • 1
    Oh doh. So the `.` is the syntax that tells python when a number is a float or int? Thank you! –  Jul 12 '13 at 19:50
  • Yeah, it's a decimal point when used with a number. – kindall Jul 12 '13 at 19:51
  • 1
    Also consider that `1.e3` is a perfectly valid float literal; how could you distinguish that from the member `e3` of the int `1` if both were allowed? – abarnert Jul 12 '13 at 19:56
  • 1
    Yeah, you'd be forced to write that as `1.0e3 or `1e3`... which would be OK I guess. – kindall Jul 12 '13 at 19:58