All Questions From This Chapter « Previously
explain this text from a book in layman terms (it's completely bouncing off of me):
The default class factory is type. When you write
class Dog:
pass
Python roughly does:
Dog = type('Dog', (), {})
This is one of those Python lines that looks mysterious until you realize it's just showing what Python does behind the scenes.
Let's start with the code you normally write:
Pythonclass Dog:
pass
Most people think this creates a class using some special Python magic.
What actually happens is that Python asks another object called type to create the class.
Behind the scenes, Python roughly does:
PythonDog = type('Dog', (), {})
Let's decode each piece.
Step 1: What is type?
You've probably used it like this:
Pythonx = 5
print(type(x))
Output:
Python<class 'int'>
So it seems like type() tells you the type of an object.
But here's the twist:
Pythonprint(type(int))
Output:
Python<class 'type'>
Even classes themselves are objects.
And the thing that creates classes is type.
Think of it like this:
Dog object <-- created by -- type
Cat object <-- created by -- type
Person object <-- created by -- type
type is the "class factory."
Step 2: The first argument
Pythontype('Dog', (), {})
First argument:
Python'Dog'
This is simply the class name.
Equivalent to:
Pythonclass Dog:
pass
Step 3: The second argument
Python()
This is a tuple containing parent classes.
Example:
Pythonclass Dog:
pass
has no explicit parent class, so:
Python()
means "no parent classes specified."
Another example:
Pythonclass Dog(Animal):
pass
would roughly become:
PythonDog = type('Dog', (Animal,), {})
Notice the tuple contains Animal.
Step 4: The third argument
Python{}
This dictionary contains everything inside the class body.
Example:
Pythonclass Dog:
species = "Canine"
def bark(self):
print("Woof")
becomes approximately:
PythonDog = type(
'Dog',
(),
{
'species': 'Canine',
'bark': bark
}
)
The dictionary is basically:
attribute name -> value
method name -> function
Visualizing the process
When Python sees:
Pythonclass Dog:
pass
it internally thinks something like:
1. Create an empty dictionary {}
2. Put class variables and methods into it
3. Call type()
4. Store the result in Dog
So:
Pythonclass Dog:
pass
is roughly:
PythonDog = type('Dog', (), {})
Proof
These two are essentially equivalent:
Normal syntax
Pythonclass Dog:
def bark(self):
print("Woof")
Using type
Pythondef bark(self):
print("Woof")
Dog = type(
'Dog',
(),
{'bark': bark}
)
Both work:
Pythond = Dog()
d.bark()
Output:
PythonWoof
The big idea
There are objects, classes, and something that creates classes.
Think of a car factory:
Factory ---> creates Cars
In Python:
type ---> creates Classes
Class ---> creates Objects
Example:
PythonDog = type('Dog', (), {})
type
↓
Dog class
↓
Dog()
↓
dog object
This idea becomes important later when learning metaclasses, because a metaclass is simply a custom replacement for type that can control how classes themselves are created.
All Questions From This Chapter « Previously

No comments:
Post a Comment