two python questions about function attrib.

Michael Chermside mcherm at destiny.com
Fri Jan 25 10:35:58 EST 2002


> python-listhello all, 
>    I am  reading the "Python ref-Man" after Tutorial , this is I read it
> second. the first time,  I can not understand it all, but I think I must read again in future. so Now , I reading. :), have a bit verbosity.
>    I have two questions about function Special attributes:
> 
>    1. what is mean of attrib func_dict or __dict__? I defined a function
> 	  Afunc()  as follow:
>        def Afunc():
> 			""" A func example"""
> 			print "Afunc" 
> 
> 	  why Afunc.__dict__ is {} ??? I do not know.
> 
>    2. like above, I can not know the attrib func_closure. in manual ,
>       this say " free variables", I did not saw it ago at all.
> 
> 		who can help me? thank u.
> 
>             sail
>             raise_sail at sina.com


I think I can answer these. My first point is that you probably don't 
really care about these things right now. The __dict__ and closure stuff 
for functions are there to support the behind-the-scenes work needed for 
some Python features, but 99% of the time you don't care about them, you 
just use the functions. Nevertheless, Python ALLOWS you to access them 
for those rare cases when you do care. So my first advice is just don't 
worry about them.

Of course a brush-off is never a satisfactory answer, so let me try to 
give the real answers also. A "closure" is when a function is created 
with kind of "memorizes" something about the environment in which it was 
defined. A toy example will help.

 >>> def incBy10(x):
	return x + 10

 >>> def incBy15(x):
	return x + 15

 >>> def incBy20(x):
	return x + 20

After you define a few of these, you get tired of repeating it and you 
might want a function to do the work for you. Something like this:

 >>> def incBy(amount):
	def incByAmount(x):
		return x + amount
	return incByAmount

 >>> incBy25 = incBy(25)
 >>> incBy30 = incBy(30)
 >>> incBy35 = incBy(35)

As the following will demonstrate, these new functions work just like 
the earlier ones you had to define one by one.

 >>> incBy30(1000)
1030

However, somehow, the incBy30 function must "remember" the value 30. 
That's called a closure, and that's whats stored in func_closure:

 >>> incBy30.func_closure
(<cell at 0x00934C40: int object at 0x00745F18>,)

I don't know exactly how to show it, but that int object at 0x00745F18 
is the number 30.

--------------

And the other question was about func.__dict__. Well, for objects, 
modules, and such things "__dict__" is a dictionary of values which be 
attached to the object/module and accessed using the "." notation. Well, 
it isn't used very often, but sometimes people want to attach some 
values to a function. You could do something like this:

 >>> def f():
	pass

 >>> def g():
	pass

 >>> f.message = 'The initialization function has failed.'
 >>> g.message = 'The database access has failed.'

The goal being to associate an "error message" with each function. This 
would only be useful if you were doing something pretty abstract -- 
perhaps building a system which automatically assembled and called the 
functions and built error handling from the .message fields. But if you 
DID want to do such a thing, it's allowed, and the message is stored in 
the __dict__ dictionary:

 >>> f.__dict__
{'message': 'The initialization function has failed.'}


Hope this helps!

-- Michael Chermside







More information about the Python-list mailing list