In Python 3, the pound symbol, £, is one character long, as you would expect.
Python 3.4.0 (default, Jun 19 2015, 14:20:21)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> len('£')
1
But in Python 2 (which I, like many people, have to use for work), it is two characters long.
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> len('£')
2
Unless you convert it to unicode.
In [152]: len(u'£')
Out[152]: 1
Worth knowing!
It's also worth being aware that 'str's get a bit funny in Python 2 when you wander out of ascii-land. And iterating through them will not do what you expect. I recommend sticking to unicode where you can.
Tuesday, October 20, 2015
Sunday, October 4, 2015
"java.lang.RuntimeException: Reader tag must be a symbol" Clojure
Very quick blog post because I couldn't see this anywhere on the top page of results:
If you're learning clojure, and you get this error
'java.lang.RuntimeException: Reader tag must be a symbol'
check if you're putting whitespace between # and ( in an anonymous function literal. Don't do that.
(If it was something else causing that error for you, please add it to the comments when you work it out)
Sunday, August 9, 2015
Object attribute spacing, floats, and .real, a Python WAT
Recently, while code reviewing a colleague's code, I spotted something like this:
object . attribute
I thought 'surely that won't work'. So I went to ipython and did something like this:
In [52]: class MyClass():
....: def __init__(self, attribute):
....: self.attribute = attribute
....:
In [53]: my_object = MyClass(3)
In [54]: my_object.attribute
Out[54]: 3
In [55]: my_object . attribute
Out[55]: 3
In [56]: my_object.attribute == my_object . attribute
Out[56]: True
Given that it appeared that python allowed any number of spaces on either side of the full stop (period, if you're speaking American English), which was news to me, I tweeted this:
The ever helpful @brandon_rhodes checked and couldn't see anyplace in pep8 that disallowed it.
A few days later I got another reply from @bmispelon
I would have expected the first two would return the same thing and the last one would give a syntax error. But in the first and third statements the full stop is interpreted as a floating point operator, so the first one gives a syntax error, the second gives 1, and the last one gives 1.0.
Knowing all that, can you predict which of the following will give syntax errors, and what the others evaluate to? I suggest using the python interpreter or ipython to check your answers. Have fun!
1) 1.real
2) 1 .real
3) 1. real
4) 1 . real
5) 1..real
6) 1 ..real
7) 1.. real
8) 1 .. real
9) 1. .real
10) 1. .real
and
11) 1 . . real
TLDR:
- The full stop in python is both the attribute access operator and the float operator.
- The attribute access operator can have any number of spaces on either side of it.
- The float operator cannot have any spaces on either side of it.
- If there is a full stop immediately after an integer, Python will interpret it as the float operator, even if there are no numbers after it, even if you meant it to be an access operator.
object . attribute
I thought 'surely that won't work'. So I went to ipython and did something like this:
In [52]: class MyClass():
....: def __init__(self, attribute):
....: self.attribute = attribute
....:
In [53]: my_object = MyClass(3)
In [54]: my_object.attribute
Out[54]: 3
In [55]: my_object . attribute
Out[55]: 3
In [56]: my_object.attribute == my_object . attribute
Out[56]: True
I wasn't expecting this
In [116]: x . thingie == x.thingie
Out[116]: True
The question is now, is it a pep8 violation my linter misses?
— SaoilĂ (@saoili) June 19, 2015
The ever helpful @brandon_rhodes checked and couldn't see anyplace in pep8 that disallowed it.
@saoili I just scanned PEP-8 and do not see anything stated about whether there need to be spaces around the attribute period or not :)
— Brandon Rhodes (@brandon_rhodes) June 19, 2015
But I recommended that my colleague get rid of it anyway because it looked a bit odd.A few days later I got another reply from @bmispelon
@saoili I thought of your tweet today when I discovered that the following three are not the same:
1.real
1 .real
1..real
— Baptiste Mispelon (@bmispelon) June 24, 2015
I would have expected the first two would return the same thing and the last one would give a syntax error. But in the first and third statements the full stop is interpreted as a floating point operator, so the first one gives a syntax error, the second gives 1, and the last one gives 1.0.
Knowing all that, can you predict which of the following will give syntax errors, and what the others evaluate to? I suggest using the python interpreter or ipython to check your answers. Have fun!
1) 1.real
2) 1 .real
3) 1. real
4) 1 . real
5) 1..real
6) 1 ..real
7) 1.. real
8) 1 .. real
9) 1. .real
10) 1. .real
and
11) 1 . . real
TLDR:
- The full stop in python is both the attribute access operator and the float operator.
- The attribute access operator can have any number of spaces on either side of it.
- The float operator cannot have any spaces on either side of it.
- If there is a full stop immediately after an integer, Python will interpret it as the float operator, even if there are no numbers after it, even if you meant it to be an access operator.
Subscribe to:
Posts (Atom)