Implicit conversion to boolean in if and while statements

Chris Angelico rosuav at
Sat Feb 9 02:17:26 CET 2013

On Sat, Feb 9, 2013 at 11:49 AM, Rick Johnson
<rantingrickjohnson at> wrote:
> On Friday, February 8, 2013 6:05:54 PM UTC-6, Chris Angelico wrote:
>> The sum builtin works happily on any sequence of objects
>> that can be added together. It works as an excellent
>> flatten() method:
>> >>> nested_list = [["q"], ["w","e"], ["r","t","u"], ["i","o","p"]]
>> >>> sum(nested_list,[])
>> ['q', 'w', 'e', 'r', 't', 'u', 'i', 'o', 'p']
>> >>> nested_list
>> [['q'], ['w', 'e'], ['r', 't', 'u'], ['i', 'o', 'p']]
> What the hell? Oh yeah, you must be using pike again. No, if it were pike the list would look like this:
> ({({"q"}), ({"w","e"}), ({"r","t","u"}), ({"i","o","p"})})
> Of course you'd have to declare it first using an /expanded/ Java syntax:
>  nested_list = array(array(string))

Strange... normally I have to actually bring Pike up, no idea why
you're doing it for me. But okay.

Actually, that's not a declaration, that's an assignment; and in Pike,
a 'type' is a thing, same as it is in Python (though not quite). If I
were to declare it in Pike, it would be:

array(array(string)) nested_list;

Though the part inside the parens can be omitted, in which case the
array can contain anything, rather than being restricted to strings.
In actual fact, Rick, despite your complaints about the syntax, it's
able to achieve exactly what you were thinking Python should do:
declare an array/list that contains only numbers.

> Folks, i couldn't make this stuff up if i wanted to. Go read for yourself if want a few laughs.

Apart from some syntactic differences, which in the scheme of things
are pretty trivial, the only difference is that Pike permits (without
demanding) you to restrict an array's members. In fact, Pike lets you
declare everything as 'mixed' if you like, allowing you to put
*anything* into *anywhere*... which, yaknow, is pretty much identical
to Python's model (only with declared variables), and takes advantage
of the fact that there are no Java-style "unboxed" types (the Pike int
is like the Py3 int or the Py2 long, arbitrary precision).

>> I'm not sure what your definition of a numeric type is, but I suspect
>> that list(str) isn't part of it.
> Of course not.

And yet, in both Pike and Python, adding lists/arrays of strings
together is not just legal but extremely useful. Do we need to rewrite
the sum() function to handle lists instead of letting operator
overloading take care of it for us? Suppose I make a string subclass
with a division operator:

class divisible_string(str):
	def __truediv__(self,n):
		return [self[pos:pos+n] for pos in range(0,len(self),n)]
# and a bunch of functions so operations on a divisible_string return
a divisible_string

Now, I should be able to divide a string by 2 and get a two-element
list. Kinda convenient, would be cool if str supported this, but it's
OOP and OOP is all about operator overloading, even if it makes your
code less readable - a language isn't 100% OOP unless this sort of
thing is normal, right?

Suppose also that I have an avg() function that does this:

def avg(it):
	for pos,val in enumerate(it):
	return tot/(pos+2)

I can take the avg() of a list (or other iterable) of integers, and
get back a number. Do I need to rewrite this function to handle my
divisible_string? Or do I need to have divisible_string subclass some
"averageable" class/interface in order to permit them to be used
inside a list that has an avg() method?


How do you solve the puzzle, Rick?


More information about the Python-list mailing list