Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is func_dict?

Tags:

python

If I make a simple function in python, it has both __dict__ and func_dict as attributes, both of which start out as empty dictionaries:

>>> def foo():
...     return 42
... 
>>> foo.__dict__
{}
>>> foo.func_dict
{}

If I add an attribute to foo, it shows up in both:

>>> foo.x = 7
>>> foo.__dict__
{'x': 7}
>>> foo.func_dict
{'x': 7}

What is the difference between these attributes? Is there a specific use-case of one over the other?

like image 462
Barry Avatar asked Jul 02 '15 18:07

Barry


1 Answers

They're aliases for the same underlying dict. You should use __dict__, since func_dict is gone in Python 3.

like image 172
user2357112 supports Monica Avatar answered Sep 21 '22 13:09

user2357112 supports Monica