[Tutor] funny true or false

Magnus Lyckå magnus@thinkware.se
Fri May 9 09:58:02 2003


At 16:29 2003-05-09 +0800, ali wrote:
>isn't this weird and may lead to confusion (of course, is your using True or
>False as variables and not as constants)...

A name is just a name is just a name in Python.
You can use it for whatever you like if want
to make life miserable. But usually, we don't,
since we prefer a calm and happy life. :)

 >>> True, False, list, str = list, str, True, False
 >>> x = False(11*13)
 >>> type(x)
<type 'str'>
 >>> x
'143'
 >>> l = True(x)
 >>> l
['1', '4', '3']
 >>> print list, str
1 0

There are lots of things that are permitted but stupid,
and there is no way the interpreter/compiler/whatever can
stop most of them. Sure, you might prevent some uncommon
mistakes, but is it meaningful to try to prevent these
kinds of unlikely errors when you can't possibly stop a
programmer from writing "a = b + c" when he should really
have written "a = b - c"?

The flexibility we have in Python in assigning whatever
name we want for whatever object we want is a very useful
and powerful ability.

Personally, I think that injecting "True, False = 'x', []"
and verifying that a program doesn't change behaviour could
be a reasonable way to make sure that True and False hasn't
been used in any ugly way...

For more about names and objects, see e.g.
http://effbot.org/zone/python-objects.htm


--
Magnus Lycka (It's really Lyck&aring;), magnus@thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The shortest path from thought to working program