How well do you know Python?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Jul 5 02:05:20 EDT 2016


On Tuesday 05 July 2016 14:02, Chris Angelico wrote:

> After some discussion with a Ruby on Rails programmer about where Ruby
> ends and where Rails begins (and it's definitely not where I'd have
> expected... Rails does a ton of monkey-patching, including of built-in
> types, to provide functionality that is strangely absent from the core
> language), I tried to come up with some somewhat-challenging Python
> questions. But to make them hard, I had to go a smidge more esoteric
> than the Ruby questions did.... Anyhow, see how you go. Assume Python
> 3.x unless stated.




S
P
O
I
L
E
R
 
S
P
A
C
E



A
N
D
 
A
 
B
I
T
 
M
O
R
E
 


> 1) Under what circumstances can str.upper() return a string of
> different length to its input?

Potentially any string might uppercase to a different length, depending on the 
content of the string and the case conversion rules for letters from that 
script. For Europeans, the simplest example is probably the German eszett, or 
\N{LATIN SMALL LETTER SHARP S}. There's an uppercase version, \N{LATIN CAPITAL 
LETTER SHARP S} but for historical reasons (and the idiosyncrasies of German) 
"ß" uppercases to "SS".


> 2) What exception do you get when you craft an impossible class hierarchy?
>     a. ValueError b. TypeError c. types.ClassInheritanceError d. SyntaxError

Without checking, I would expect TypeError (90% confident) or SyntaxError 
(unlikely).


> 3) What does `from __future__ import braces` do?

Report your IP address and username to a secret database of people who will be 
sent to the gulags for re-education when the revolution comes. Or at least, 
they would be, if the Python Secret Underground, which most definitely doesn't 
exist, existed, which it doesn't.

Otherwise it prints the final word on the question of braces in Python.


> 4) Which operator, removed from Python 3.0, can be reinstated with a
> 'joke' future directive?

The <> not-equal operator.


> 5) What is the difference between the `/` and `//` operators in Python
> 2.7? In Python 3.x?

In Python 3, / means "true division" (i.e. what a calculator will do), and // 
means "integer division". E.g. 11/2 gives 5.5, while 11//2 gives 5.

In 2.7, there are two cases: with, or without "from __future__ import 
division". With the __future__ directive, they mean the same as in Python 3. 
Without, // means "integer division", but / depends on its arguments. If both 
arguments are ints, it performs integer division. If at least one is a float, 
it performs "true division" like a calculator.

Although to be pedantic, in all cases it depends on operator overloading :-)



-- 
Steve




More information about the Python-list mailing list