Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Challenge: Inspecting Class Hierarchies | Metaclasses Fundamentals
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Python Metaclasses Demystified

bookChallenge: Inspecting Class Hierarchies

When you want to understand how Python classes and metaclasses relate, you can use the __class__ attribute and the type() function. Every object in Python has a __class__ attribute that points to its class. For classes themselves, __class__ refers to the metaclass, which is usually type unless a custom metaclass is specified. The type() function, when called on an object, returns its class; when called on a class, it returns its metaclass. To explore the base classes of a class, you can use the __bases__ attribute, which is a tuple of the class's immediate parent classes. By combining these tools, you can inspect class hierarchies and metaclass relationships for both user-defined and built-in types.

Завдання

Swipe to start coding

Write a function that takes a single argument obj.

  • Print the class of obj in the format <class 'module.qualname'>.
  • Print the base classes of obj's class, or "N/A" if obj is an instance of a built-in type.
  • Print the metaclass of obj's class.

Use the __class__ attribute to get the class of an object. Only print the base classes if the object's class is not a built-in type. The metaclass of a class can be accessed using the __class__ attribute of the class itself.

Рішення

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 2
single

single

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you give an example showing how to use `__class__`, `type()`, and `__bases__`?

What is a metaclass, and when would I use a custom one?

How do these concepts apply to built-in types like `int` or `list`?

close

bookChallenge: Inspecting Class Hierarchies

Свайпніть щоб показати меню

When you want to understand how Python classes and metaclasses relate, you can use the __class__ attribute and the type() function. Every object in Python has a __class__ attribute that points to its class. For classes themselves, __class__ refers to the metaclass, which is usually type unless a custom metaclass is specified. The type() function, when called on an object, returns its class; when called on a class, it returns its metaclass. To explore the base classes of a class, you can use the __bases__ attribute, which is a tuple of the class's immediate parent classes. By combining these tools, you can inspect class hierarchies and metaclass relationships for both user-defined and built-in types.

Завдання

Swipe to start coding

Write a function that takes a single argument obj.

  • Print the class of obj in the format <class 'module.qualname'>.
  • Print the base classes of obj's class, or "N/A" if obj is an instance of a built-in type.
  • Print the metaclass of obj's class.

Use the __class__ attribute to get the class of an object. Only print the base classes if the object's class is not a built-in type. The metaclass of a class can be accessed using the __class__ attribute of the class itself.

Рішення

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 2
single

single

some-alt