None assigment

D-Man dsh8290 at rit.edu
Thu Feb 8 12:36:00 EST 2001


On Thu, Feb 08, 2001 at 06:12:27PM +0100, Gregoire Welraeds wrote:
| 
| > There's nothing special about the name "None"; it's just a variable,
| > like any other. 
| <snip>
| > After doing "None = 2", you can "del None" to get the default value 
| > back.
| 
| If we follow that logic, I could use any non assigned variable to have the
| following working:
| 
| >>> a= [1,'',3]
| >>> filter(b,a)
| 
| but this won't work as the interpreter complains that there is no variable
| named b.

This is because the interpreter tries to evaluate all expressions
before the actual call to the functions.  When the interpreter tries
to evaluate the expression  b  it can't.  When it evaluates the
expression  None  it can.  If you were to "del None" , then try and
pass it to a function it would fail the same way.  (Or instead of
using the del statement, just remove the name None from the
dictionary)

| 
| 	
| And finally, to add to the confusion, I have try the following:
| 
| >>> a= [1,'',3]
| >>> filter(0,a)
| 
| for what the system complains that 0 is a non function type ! 

Correct.

| 
| Now If we resume:
| None is a value
| None is a built in type
| None is an object
| None is an alias (as I understand it) for lambda x: x in the case of
| filter(None, some_list)
| 
| Now that I look at this problem, I'm totally confused about None. Any

"None" is a name/binding.  By default it refers to an object whose
type is called "None".  This object is a Singleton, and is built in.

Objects can have many properties, one of them is "callable".  If you
were to make a replacement class for None, you could do something like:

class MyNone :
	def __call__( self , arg ) :
		return arg


If I try this :

n = MyNone()
filter( n , [ 1 , 2 , 3 ] )


it would be the same as using the builtin None object.  (Unless I
missed some sublties)

None is simply an object, provided at interpreter startup, that has
some (special) properties.  (All classes define new objects, and each
has "special" properties)

| explanaition is welcome. Anyway, I don't understand that one can override
| None. Could you give me at least one single good reason to do that.

Being able to "override" None is simply how the language works.
Python allows you to assign an object (everything in python is an
object!) to any name.  "None" isn't a special case.  I can't think of
any good reason to assign a new object to "None", but that doesn't
mean the language disallows it.  (Ab)using None in this manner would
certainly obfuscate your code.

I also don't think it is really necessary for the interpreter to
disallow assignment to None since it adds complexity to the
interpreter's workings without providing any real advantage.  Instead,
programmer discipline to use None appropriately works just fine IMO. :-)

-D





More information about the Python-list mailing list