Project design
- Python course
- Expert
- Lesson 43/47
- 6 min
Design the expert final skeleton.
Expert final: data processing toolkit
In this five-lesson expert final you will build a mini application that combines every expert topic. In the first lesson you create a dynamic Tool class with type().
The Tool class will have a name attribute and a run method. In later lessons you will add metaprogramming, multiprocessing and ctypes layers on top of this base. Each lesson adds one piece with a code step; the starting code includes the previous lesson's solution. This shows how a real toolkit grows.
Practice Build the Tool class with type()
Build the Tool class with type(). The second argument is the empty tuple (), the third is a dict. The dict should have "name": "arac" and a "run" method. run returns f"{self.name} calisti". Create an instance and print run().
Starter code
# Build the Tool class with type()
# add name and run
# print the instanceSample solution
# Build the Tool class with type()
Tool = type("Tool", (), {
"name": "arac",
"run": lambda self: f"{self.name} calisti",
})
t = Tool()
print(t.run())Expected output
arac calisti
Next step: metaprogramming
The Tool class is ready. In the next lesson you will add a metaprogramming layer on top: a Field descriptor and a ModelMeta metaclass.
This layer will let classes collect their fields automatically. You will build a declarative structure similar to the Django ORM. Each lesson adds a layer; the starting code carries the previous solution.
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is type() used for in this project?
- To process an array
- To start a process
- To load a C library
- To create the dynamic Tool class Correct answer
Explanation: You create the Tool class at runtime with type().
2 Complete the Tool definition
Tool = type("Tool", (), {____})
Tool = type("Tool", (), {"name": "arac"})
Explanation: The name attribute is defined this way in the dict.
3 Match project concepts
| Term | Meaning |
|---|---|
type() |
Creates a class |
Tool |
First class |
name |
Attribute |
run |
Method |