EuroPython 2016

Dynamic Class Generation in Python

Speaker(s) Kyle Knapp

This talk is about the concept of dynamic class generation in python. The whole idea is writing code that generates classes and their functionality at runtime. You now may be asking yourself, “That sounds like a neat trick. Why would I ever generate my classes at runtime?” Here are a few reasons why:

• It can decrease the physical size of your code.

• It can improve the workflow in adding new functionality.

• It can improve reliability of your code.

One example where the power of this concept has really been leveraged is in boto3, the AWS SDK for Python. Dynamic class generation has allowed boto3 to become heavily data driven such that most of its classes and methods are generated based off JSON models representing aspects of an AWS service’s API. For example, to add support for a new AWS service API in boto3, just plop in a JSON file into the library with no additional Python code required.

Using lessons and techniques drawn from developing boto3, this talk will dive into the following topics related to dynamic class generation:

• The basics of dynamic class generation such as how to effectively dynamically generate classes.

• How to overcome some of the challenges of dynamic class generation.

• The tradeoffs in dynamically generating classes and discussion on when it is appropriate.

By the end of this talk, the hope is that you will have a better understanding of dynamic class generation and come away with helpful ideas for your next big project.

in on Monday 18 July at 12:00 See schedule

Do you have some questions on this talk?

New comment