How To: Extend Builtins In Python (2 Min) | Inherit Built In Classes

Опубликовано: 31 Март 2022
на канале: Gokce DB
218
4

In this tutorial, you'll learn how to extend builtins in Python.


Facebook:   / gokcedbsql  

Video Transcript:

Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn how to extend built-ins in Python. Let's start by looking at the DB table list class.

On line two, I'm inheriting from the built-in class by passing the list as the first parameter. I'm defining the find method and on line four I'm creating a match underscore list which is an instance of the list class. On line five, I'm creating f for loop and looping through all the DB table objects.

Next, I'm checking if the name exists in DB underscore table dot name the append it to match the underscore list. Now let's look at the UML diagram to see how we are able to access the append method. In this diagram, you can see that the DB table list class is inherited from the built-in list class.

If I turn on the methods view you can see there's an append method that exists in the list class and that's why it's also being extended to the DB list class. Let's look at the DB table. Next on line twelve, I'm creating a table underscore list object which is an instance of the DB table list class.

Line fourteen, I'm defining the special underscore underscore init underscore underscore method to initialize the name instance variable. On line sixteen. I'm appending myself to the table underscore list.

Starting line nineteen, I'm creating three objects which are the instance of the DB table class. Finally, on line twenty-four, I'm printing all the table names which have the word property in them. Let's execute this program to see what the output looks like.

If I change the search keyword to the customer then we should only expect to see one result in the output. There you have it. Make sure you like, subscribe, and turn on the notification bell. Until next time.



DBTableList inherits from the built-in list data type/class
class DBTableList(list):
def find(self, name):
match_list = list()
for db_table in self:
if name in db_table.name:
match_list.append(db_table)
return match_list


class DBTable:
table_list = DBTableList()

def __init__(self, name):
self.name = name
DBTable.table_list.append(self)


t1 = DBTable("property")
t2 = DBTable("property_sales")
t3 = DBTable("customer")

List comprehension syntax
[print(table.name) for table in DBTable.table_list.find('customer')]


Смотрите видео How To: Extend Builtins In Python (2 Min) | Inherit Built In Classes онлайн, длительностью часов минут секунд в хорошем качестве, которое загружено на канал Gokce DB 31 Март 2022. Делитесь ссылкой на видео в социальных сетях, чтобы ваши подписчики и друзья так же посмотрели это видео. Данный видеоклип посмотрели 218 раз и оно понравилось 4 посетителям.