Issue1413
Created on 2007-11-09 14:43 by mykhal, last changed 2022-04-11 14:56 by admin. This issue is now closed.
| Messages (12) | |||
|---|---|---|---|
| msg57304 - (view) | Author: Michal Božoň (mykhal) | Date: 2007-11-09 14:43 | |
It's impossible to call methods of int literals directly e.g.
1.__str__() (the same for oct literals).
Even through it works for float, hex, literals, etc..
>>> 0x1.__str__()
'1'
>>> 1e0.__str__()
'1.0'
>>> 1..__str__()
'1.0'
>>> hasattr(1, '__str__')
True
>>> 1.__str__()
File "<stdin>", line 1
1.__str__()
^
SyntaxError: invalid syntax
>>> 01.__str__()
File "<stdin>", line 1
01.__str__()
^
SyntaxError: invalid syntax
|
|||
| msg57308 - (view) | Author: Michal Božoň (mykhal) | Date: 2007-11-09 15:15 | |
.. OK, now I see than (1).__str__() works.. however, could be the parser fixed to 1.__str__() work too ? |
|||
| msg57323 - (view) | Author: Christian Heimes (christian.heimes) * ![]() |
Date: 2007-11-09 20:12 | |
It's a tricky problem because it's ambiguous: >>> 1.j What's going to happen here? Does it do getattr(1, 'j') or does it create the imaginary number 1j? |
|||
| msg57324 - (view) | Author: Michal Božoň (mykhal) | Date: 2007-11-09 20:33 | |
I don't understand why 1.j is 1j .. because there's no int.j .. why then 1.L is not 1L ? |
|||
| msg57325 - (view) | Author: Michal Božoň (mykhal) | Date: 2007-11-09 20:39 | |
.. however, fixing this is not necessary - because no one would probably use it, it's just a syntax inconsistency |
|||
| msg57326 - (view) | Author: Michal Božoň (mykhal) | Date: 2007-11-09 20:41 | |
(finally now I get it.. I have forgotten that complex numbers can be float.. :) sorry ) |
|||
| msg57327 - (view) | Author: Christian Heimes (christian.heimes) * ![]() |
Date: 2007-11-09 20:46 | |
> I don't understand why 1.j is 1j .. because there's no int.j .. why then > 1.L is not 1L ? Complex numbers from the number domain |C which supersets |R. Complex numbers are usually implemented and viewed as 2d vectors made from two floating numbers real and img. A long or long int however is from number domain |Z which doesn't allow fraction and hence no '.', too. It should explain why 1.L is illegal but 1.j is 1.0 * 1j. > .. however, fixing this is not necessary - because no one would > probably use it, it's just a syntax inconsistency I don't if it's possible to fix the problem with Python's parser. However the inconsistency should be documented. Is it explained in the docs and do the docs also mention the (1).__attr__ trick? I wasn't aware that it works. Christian |
|||
| msg57328 - (view) | Author: Michal Božoň (mykhal) | Date: 2007-11-09 20:52 | |
I don't know it's in docs, it came into my mind, maybe logically (but later) to put 1 into parentheses |
|||
| msg57329 - (view) | Author: Michal Božoň (mykhal) | Date: 2007-11-09 20:54 | |
.. I remember.. it came onto my mind when I tried also -1.__str__() and found out that the dot has higher priority than unary minus :) |
|||
| msg57334 - (view) | Author: Martin v. Löwis (loewis) * ![]() |
Date: 2007-11-09 21:29 | |
As now should be clear, there really is no bug here. Notice that you can also write 1 .__str__() |
|||
| msg57335 - (view) | Author: Michal Božoň (mykhal) | Date: 2007-11-09 21:38 | |
interesting. I'm not sure I've read anywhere that it is allowed to place a whitespace between object and attributes. Thanks |
|||
| msg57340 - (view) | Author: Martin v. Löwis (loewis) * ![]() |
Date: 2007-11-09 23:28 | |
See http://www.python.org/doc/2.5/ref/whitespace.html which says that you can put spaces between arbitrary tokens, and http://www.python.org/doc/2.5/ref/attribute-references.html which says that all of primary, ".", and identifier are separate tokens in an attributeref (not so in a literal, where the . is part of the floating point literal). |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022-04-11 14:56:28 | admin | set | github: 45754 |
| 2007-11-09 23:28:37 | loewis | set | messages: + msg57340 |
| 2007-11-09 21:38:40 | mykhal | set | messages: + msg57335 |
| 2007-11-09 21:29:41 | loewis | set | status: open -> closed resolution: not a bug messages: + msg57334 nosy: + loewis |
| 2007-11-09 20:54:59 | mykhal | set | messages: + msg57329 |
| 2007-11-09 20:52:30 | mykhal | set | messages: + msg57328 |
| 2007-11-09 20:46:28 | christian.heimes | set | messages: + msg57327 |
| 2007-11-09 20:41:41 | mykhal | set | messages: + msg57326 |
| 2007-11-09 20:39:13 | mykhal | set | messages: + msg57325 |
| 2007-11-09 20:33:10 | mykhal | set | messages: + msg57324 |
| 2007-11-09 20:12:01 | christian.heimes | set | nosy:
+ christian.heimes messages: + msg57323 |
| 2007-11-09 15:15:08 | mykhal | set | messages: + msg57308 |
| 2007-11-09 14:43:27 | mykhal | create | |
