Frame Hack Exercises

Remove object as the superclass of PyEmployee. How does the output change? Why does it make a difference if PyEmployee doesn't inherit from object?


Change the properties function of Recipe #2 to match the code below. Does it still work as expected? Why or why not?

def properties(**kwargs):
    framedict = sys._getframe(1).f_locals

    for propname, realname in kwargs.items():
        def get(self):
            return getattr(self.e, 'Get'+realname)()

        def set(self, value):
            getattr(self.e, 'Set'+realname)(value)

        framedict[propname] = property(get, set)

Implement the property generation functionality, this time using a metaclass. Make sure the following code behaves identically to our frame hack-based code:

class __autoprops__(type):
    "Define this metaclass"

class PyEmployee(object):
    __metaclass__ = __autoprops__

    def __init__(self, given_name, family_name, date_of_birth):
        self.e = Employee()
        self.given = given_name
        self.family = family_name
        self.birth = date_of_birth

    properties = {
        'given':  'GivenName',
        'family': 'FamilyName',
        'birth':  'DateOfBirth',
    }

Do you want to understand frame hacks by learning from the masters themselves? Here are some Python modules that use frame hacks:


[Advanced] Write two functions that print the last several frames, then run them multi-threaded 100 times and analyze the nature of frame tracing in a multi-threaded environment. I just wanted to write down this thought before I forgot!