I was reading about introspection in Python 2.7 and came across code similar to what is shown in Figure 1: class Bag. It turns out that it is a very interesting passage of code for me as it uses many of what I would consider intermediate Python techniques. In Part I of this blog post, I will cover some of the interesting Python constructs that crossed my mind when looking at the code. Part II is a short explanation of introspection in Python 2.7.
1 class Bag:
2 def __init__(self, **d):
3 for k,v in d.iteritems():
4 exec("self.%s = %s" % (k,v))
Figure 1: class Bag (figure numbers are only for reference)