[Tutor] __class__.__name__ for literal integer causes SyntaxError

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[Tutor] __class__.__name__ for literal integer causes SyntaxError

Tim Johnson-18
See the following console session:
>>> 4.6.__class__.__name__
'float'
>>> 6.__class__.__name__
  File "<stdin>", line 1
    6.__class__.__name__
              ^
SyntaxError: invalid syntax
>>> x = 6
>>> x.__class__.__name__
'int'
>>> "me".__class__.__name__
'str'
I note that the reference to '__class__.__name__' for string and
float literals is executed, but that there is a SyntaxError for that
same reference of a 'int' literal.

I'd welcome comments, explanations or URLs to discussions.
thanks
--
Tim
tim at tee jay forty nine dot com or akwebsoft dot com
http://www.akwebsoft.com
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] __class__.__name__ for literal integer causes SyntaxError

Emile van Sebille
On 4/4/2012 6:27 PM Tim Johnson said...
> See the following console session:
>>>> 4.6.__class__.__name__

The first decimal is considered to be part of the float literal here...


> 'float'
>>>> 6.__class__.__name__

... _and_ here...

>    File "<stdin>", line 1
>      6.__class__.__name__
>                ^
> SyntaxError: invalid syntax

... which explains the error -- the float value is improper.

>>>> x = 6
>>>> x.__class__.__name__
> 'int'
>>>> "me".__class__.__name__
> 'str'
> I note that the reference to '__class__.__name__' for string and
> float literals is executed, but that there is a SyntaxError for that
> same reference of a 'int' literal.
>
> I'd welcome comments, explanations or URLs to discussions.
> thanks

Try

 >>> (6).__class__.__name__
'int'
 >>>

HTH,

Emile

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] __class__.__name__ for literal integer causes SyntaxError

Dave Angel-3
On 04/04/2012 10:27 PM, Emile van Sebille wrote:

> On 4/4/2012 6:27 PM Tim Johnson said...
>> See the following console session:
>>>>> 4.6.__class__.__name__
>
> The first decimal is considered to be part of the float literal here...
>
>
>> 'float'
>>>>> 6.__class__.__name__
>
> ... _and_ here...
>
>>    File "<stdin>", line 1
>>      6.__class__.__name__
>>                ^
>> SyntaxError: invalid syntax
>
> ... which explains the error -- the float value is improper.
>
>>>>> x = 6
>>>>> x.__class__.__name__
>> 'int'
>>>>> "me".__class__.__name__
>> 'str'
>> I note that the reference to '__class__.__name__' for string and
>> float literals is executed, but that there is a SyntaxError for that
>> same reference of a 'int' literal.
>>
>> I'd welcome comments, explanations or URLs to discussions.
>> thanks
>
> Try
>
> >>> (6).__class__.__name__
> 'int'
> >>>
>
> HTH,
>
> Emile

Emile gave you the explanation.  But another way to convince the parser
to treat the decimal point differently is to leave a space(s) between
the number  and the separator.

>>> 6.__class__.__name__
  File "<stdin>", line 1
    6.__class__.__name__
              ^
SyntaxError: invalid syntax

>>> 6  .__class__.__name__
'int'
>>>



--

DaveA

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor