ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.2 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://python.pendancer.com/advanced/metaclasses.html |
| Last Crawled | 2026-04-18 13:34:06 (6 days ago) |
| First Indexed | 2025-01-18 07:19:57 (1 year ago) |
| HTTP Status Code | 200 |
| Content | |
| Meta Title | Metaclasses - Python |
| Meta Description | Python course for the 90th Cyberspace Operations Squadron |
| Meta Canonical | null |
| Boilerpipe Text | Defined as “the class of a class”.
Any class whose instances are themselves classes.
type is a metaclass for instance; it creates classes!
Used to construct classes. (always happens under hood)
Can create dynamic classes with less lines using type.
Think: instances are to classes as classes are to metaclasses.
To create a metaclass, create a class that inherits from type
# in the most simple form
class Meta(type):
pass
Classes are normally created with the type metaclass
We can create a class with a different metaclass:
# Python 2
class Final(object):
__metaclass__ = Meta
# Python 3
class Final(metaclass=Meta):
pass
__new__():
It’s a method which is called before __init__(). It creates the object and return it.
__init__():
This method just initialize the created object passed as parameter
Example:
class Meta(type):
def __init__(cls, name, bases, dct):
print "Creating class {} using Meta".format(name)
super(Meta, cls).__init__(name, bases, dct)
class Foo(object):
__metaclass__ = Meta
class FooBar(Foo):
pass
First we create a new metaclass called
Meta
:
On the construction of classes built using Meta, we print out that we are creating a class using Meta.
Next, create a class called Foo… built from Meta rather than type.
Finally, create a FooBar class from Foo.
Notice how it too was built using Meta?
End of Python Subject |
| Markdown | 1. [Introduction](https://python.pendancer.com/index.html)
2. [**1\.** C-4105L-TM Python Features](https://python.pendancer.com/python_features/index.html)
3. 1. [**1\.1.** Objectives](https://python.pendancer.com/python_features/objectives.html)
2. [**1\.2.** Introduction to Python](https://python.pendancer.com/python_features/python_intro.html)
3. [**1\.3.** PyDocs & PEP8](https://python.pendancer.com/python_features/pydocs_pep8.html)
4. [**1\.4.** Objects](https://python.pendancer.com/python_features/objects.html)
5. 1. [**1\.4.1.** Lab 1A](https://python.pendancer.com/python_features/lab1a.html)
6. [**1\.5.** Py2 vs Py3 Differences](https://python.pendancer.com/python_features/py2_py3.html)
7. [**1\.6.** Running Python](https://python.pendancer.com/python_features/running_python.html)
4. [**2\.** C-4110L-TM Python Variables and Data Types](https://python.pendancer.com/Data_Types/index.html)
5. 1. [**2\.1.** Objectives](https://python.pendancer.com/Data_Types/objectives.html)
2. [**2\.2.** Variables](https://python.pendancer.com/Data_Types/variables.html)
3. 1. [**2\.2.1.** Lab 2A](https://python.pendancer.com/Data_Types/lab2a.html)
4. [**2\.3.** Numbers](https://python.pendancer.com/Data_Types/numbers.html)
5. 1. [**2\.3.1.** Lab 2B & Lab2C](https://python.pendancer.com/Data_Types/lab2b_c.html)
6. [**2\.4.** Strings](https://python.pendancer.com/Data_Types/strings.html)
7. 1. [**2\.4.1.** Lab 2D & Lab2E](https://python.pendancer.com/Data_Types/lab2d_e.html)
8. [**2\.5.** Lists](https://python.pendancer.com/Data_Types/lists.html)
9. 1. [**2\.5.1.** Lab 2F](https://python.pendancer.com/Data_Types/lab2f.html)
10. [**2\.6.** Bytes and Bytearray](https://python.pendancer.com/Data_Types/byte_array.html)
11. 1. [**2\.6.1.** Lab 2G](https://python.pendancer.com/Data_Types/lab2g.html)
12. [**2\.7.** Tuples, range & buffer](https://python.pendancer.com/Data_Types/tuples.html)
13. [**2\.8.** Dictionaries & Sets](https://python.pendancer.com/Data_Types/mapping.html)
14. 1. [**2\.8.1.** Lab 2H](https://python.pendancer.com/Data_Types/lab2h.html)
6. [**3\.** C-4115L-TM Python Operators and Flow Control](https://python.pendancer.com/Flow_Control/index.html)
7. 1. [**3\.1.** Objectives](https://python.pendancer.com/Flow_Control/objectives.html)
2. [**3\.2.** Operators](https://python.pendancer.com/Flow_Control/operators.html)
3. [**3\.3.** I/O Print](https://python.pendancer.com/Flow_Control/io_print.html)
4. 1. [**3\.3.1.** Lab 3A](https://python.pendancer.com/Flow_Control/lab3a.html)
5. [**3\.4.** I/O: Files](https://python.pendancer.com/Flow_Control/io_files.html)
6. 1. [**3\.4.1.** Lab 3B](https://python.pendancer.com/Flow_Control/lab3b.html)
7. [**3\.5.** If, Elif, Else](https://python.pendancer.com/Flow_Control/if_elif_else.html)
8. 1. [**3\.5.1.** Lab 3C](https://python.pendancer.com/Flow_Control/lab3c.html)
9. [**3\.6.** While Loops](https://python.pendancer.com/Flow_Control/while_loops.html)
10. 1. [**3\.6.1.** Lab 3D](https://python.pendancer.com/Flow_Control/lab3d.html)
11. [**3\.7.** For Loops](https://python.pendancer.com/Flow_Control/for_loops.html)
12. 1. [**3\.7.1.** Lab 3E](https://python.pendancer.com/Flow_Control/lab3e.html)
13. [**3\.8.** Break and Continue](https://python.pendancer.com/Flow_Control/break_continue.html)
14. 1. [**3\.8.1.** Lab 3F](https://python.pendancer.com/Flow_Control/lab3f.html)
8. [**4\.** C-4120L-TM Python Functions](https://python.pendancer.com/functions/index.html)
9. 1. [**4\.1.** Objectives](https://python.pendancer.com/functions/objectives.html)
2. [**4\.2.** Scope](https://python.pendancer.com/functions/scope.html)
3. [**4\.3.** User Functions](https://python.pendancer.com/functions/user_functions.html)
4. [**4\.4.** Lambda Functions](https://python.pendancer.com/functions/lambda_functions.html)
5. 1. [**4\.4.1.** Lab 4A & 4B](https://python.pendancer.com/functions/lab4a.html)
6. [**4\.5.** List Comprehension](https://python.pendancer.com/functions/list_comprehension.html)
7. [**4\.6.** Recursion](https://python.pendancer.com/functions/recursion.html)
8. [**4\.7.** Closures, Iterators & Generators](https://python.pendancer.com/functions/closures_iterators_generators.html)
10. [**5\.** C-4125L-TM Python Object Oriented Programming](https://python.pendancer.com/oop/index.html)
11. 1. [**5\.1.** Objectives](https://python.pendancer.com/oop/objectives.html)
2. [**5\.2.** Modules](https://python.pendancer.com/oop/modules.html)
3. [**5\.3.** Packages](https://python.pendancer.com/oop/packages.html)
4. 1. [**5\.3.1.** Lab5A](https://python.pendancer.com/oop/lab5a.html)
5. [**5\.4.** User Classes Pt1](https://python.pendancer.com/oop/user_classes.html)
6. 1. [**5\.4.1.** Lab5B](https://python.pendancer.com/oop/lab5b.html)
7. [**5\.5.** User Classes Pt2](https://python.pendancer.com/oop/user_classes_pt2.html)
8. 1. [**5\.5.1.** Lab5C](https://python.pendancer.com/oop/lab5c.html)
9. [**5\.6.** Exceptions](https://python.pendancer.com/oop/exceptions.html)
10. [**5\.7.** OOP Principles](https://python.pendancer.com/oop/oop_principles.html)
11. [**5\.8.** OOP Terminology Review \[Bonus Lab\]](https://python.pendancer.com/oop/oop_terminology.html)
12. 1. [**5\.8.1.** Lab5D](https://python.pendancer.com/oop/lab5d.html)
12. [**6\.** C-4130L-TM Python Algorithms](https://python.pendancer.com/Algorithms/index.html)
13. 1. [**6\.1.** Objectives](https://python.pendancer.com/Algorithms/objectives.html)
2. [**6\.2.** Algorithms Analysis](https://python.pendancer.com/Algorithms/Algorithm_Analysis.html)
3. [**6\.3.** Algorithms Design](https://python.pendancer.com/Algorithms/Algorithm_Design.html)
4. [**6\.4.** Big O Notation](https://python.pendancer.com/Algorithms/Big_O_notation.html)
5. [**6\.5.** Big O Analysis](https://python.pendancer.com/Algorithms/Big_O_Analysis_Demos.html)
6. 1. [**6\.5.1.** Lab 6A](https://python.pendancer.com/Algorithms/Big_0_Analysis_Perf_Labs.html)
7. [**6\.6.** Searching Algorithms](https://python.pendancer.com/Algorithms/Searching_Algorithms.html)
8. [**6\.7.** Sorting Algorithms](https://python.pendancer.com/Algorithms/Sort.html)
14. [**7\.** C-4130L-TM Python Data Structures](https://python.pendancer.com/Data_Structures/index.html)
15. 1. [**7\.1.** Objectives](https://python.pendancer.com/Data_Structures/objectives.html)
2. [**7\.2.** List Pointer Structures](https://python.pendancer.com/Data_Structures/Lists_Pointer_Structures.html)
3. 1. [**7\.2.1.** Singly Linked List](https://python.pendancer.com/Data_Structures/Singly_Linked_List.html)
2. [**7\.2.2.** Doubly Linked List](https://python.pendancer.com/Data_Structures/Doubly_Linked_List.html)
3. [**7\.2.3.** Linked List Demonstration](https://python.pendancer.com/Data_Structures/Linked_List_Demo.html)
4. [**7\.2.4.** Lab 6B](https://python.pendancer.com/Data_Structures/Linked_List_Perf_Lab.html)
5. [**7\.2.5.** Lab 6C](https://python.pendancer.com/Data_Structures/Singly_Linked_List_Lab.html)
4. [**7\.3.** Stacks](https://python.pendancer.com/Data_Structures/Stacks_Lesson.html)
5. [**7\.4.** Stacks Implementation](https://python.pendancer.com/Data_Structures/Stack_Implementation_Lesson.html)
6. [**7\.5.** Queue](https://python.pendancer.com/Data_Structures/Queue_Lesson.html)
7. 1. [**7\.5.1.** FIFO Queue](https://python.pendancer.com/Data_Structures/FIFO_Queue.html)
2. [**7\.5.2.** Queue Demonstration 1](https://python.pendancer.com/Data_Structures/Queue_demo_lab.html)
3. [**7\.5.3.** Queue Demonstration 2](https://python.pendancer.com/Data_Structures/Queue_Demo_lab_2.html)
4. [**7\.5.4.** Lab 6D](https://python.pendancer.com/Data_Structures/Queue_Perf_Lab.html)
8. [**7\.6.** Trees](https://python.pendancer.com/Data_Structures/Trees_Lesson.html)
9. 1. [**7\.6.1.** Trees Example](https://python.pendancer.com/Data_Structures/Trees_Examples_Trees.html)
2. [**7\.6.2.** Tree Led Demonstration](https://python.pendancer.com/Data_Structures/Tree_Led_Demos.html)
3. [**7\.6.3.** Lab 6E](https://python.pendancer.com/Data_Structures/Trees_Perf_Labs.html)
16. [**8\.** C-4135L-TM Python Advanced Concepts](https://python.pendancer.com/advanced/index.html)
17. 1. [**8\.1.** Objectives](https://python.pendancer.com/advanced/objectives.html)
2. [**8\.2.** CTypes and Structures](https://python.pendancer.com/advanced/ctypes.html)
3. [**8\.3.** Regular Expressions](https://python.pendancer.com/advanced/regular_expressions.html)
4. [**8\.4.** Additional Libraries and Modules](https://python.pendancer.com/advanced/additional_libraries_modules.html)
5. [**8\.5.** Multithreading](https://python.pendancer.com/advanced/multithreading.html)
6. [**8\.6.** UnitTesting](https://python.pendancer.com/advanced/unit_testing.html)
7. [**8\.7.** Metaclasses](https://python.pendancer.com/advanced/metaclasses.html)
8. [**8\.8.** Design Patterns](https://python.pendancer.com/advanced/designpatterns.html)
9. [**8\.9.** Python Virtual Environment](https://python.pendancer.com/advanced/virtualenv.html)
18. [C-4105L-TM Python Features - Part I](https://python.pendancer.com/python_features/part_i.html)
19. [C-4110L-TM PythonVariables and Data Types - Part I](https://python.pendancer.com/Data_Types/part_i.html)
20. [C-4115L-TM PythonOperators and Flow Control - Part I](https://python.pendancer.com/Flow_Control/part_i.html)
21. [C-4120L-TM Python Functions - Part I](https://python.pendancer.com/functions/part_i.html)
22. [C-4125L-TM Python Object Oriented Programming - Part I](https://python.pendancer.com/oop/part_i.html)
23. [C-4130L-TM Python Algorithms - Part I](https://python.pendancer.com/Algorithms/part_i.html)
24. [C-4130L-TM Python Data Structures - Part I](https://python.pendancer.com/Data_Structures/part_i.html)
25. [C-4135L-TM Python Advanced Concepts - Part I](https://python.pendancer.com/advanced/part_i.html)
- Light (default)
- Rust
- Coal
- Navy
- Ayu
# Python
# [Metaclasses](https://python.pendancer.com/advanced/metaclasses.html#metaclasses)

**Defined as “the class of a class”.**
- Any class whose instances are themselves classes.
- type is a metaclass for instance; it creates classes\!
- Used to construct classes. (always happens under hood)
- Can create dynamic classes with less lines using type.
- Think: instances are to classes as classes are to metaclasses.
- To create a metaclass, create a class that inherits from type
```
# in the most simple form
class Meta(type):
pass
```
- Classes are normally created with the type metaclass
- We can create a class with a different metaclass:
```
# Python 2
class Final(object):
__metaclass__ = Meta
# Python 3
class Final(metaclass=Meta):
pass
```
**\_\_new\_\_():** It’s a method which is called before \_\_init\_\_(). It creates the object and return it.
**\_\_init\_\_():** This method just initialize the created object passed as parameter
**Example:**
```
class Meta(type):
def __init__(cls, name, bases, dct):
print "Creating class {} using Meta".format(name)
super(Meta, cls).__init__(name, bases, dct)
class Foo(object):
__metaclass__ = Meta
class FooBar(Foo):
pass
```
First we create a new metaclass called **Meta**:
- On the construction of classes built using Meta, we print out that we are creating a class using Meta.
Next, create a class called Foo… built from Meta rather than type.
Finally, create a FooBar class from Foo.
- Notice how it too was built using Meta?
***
**End of Python Subject** |
| Readable Markdown | null |
| ML Classification | |
| ML Categories | null |
| ML Page Types | null |
| ML Intent Types | null |
| Content Metadata | |
| Language | en |
| Author | null |
| Publish Time | not set |
| Original Publish Time | 2025-01-18 07:19:57 (1 year ago) |
| Republished | No |
| Word Count (Total) | 600 |
| Word Count (Content) | 213 |
| Links | |
| External Links | 0 |
| Internal Links | 108 |
| Technical SEO | |
| Meta Nofollow | No |
| Meta Noarchive | No |
| JS Rendered | No |
| Redirect Target | null |
| Performance | |
| Download Time (ms) | 484 |
| TTFB (ms) | 484 |
| Download Size (bytes) | 5,553 |
| Shard | 116 (laksa) |
| Root Hash | 848193091939660316 |
| Unparsed URL | com,pendancer!python,/advanced/metaclasses.html s443 |