کلاس متا در کد زیر دقیقا چه کاری رو انجام می دهد و تفاوت آن با متاکلاس پایتون چیست؟

پرسیده شده
فعالیت 1174 روز پیش
دیده شده 833 بار
0

کلاس متا در کد زیر دقیقا چه کاری رو انجام می دهد و تفاوت آن با متاکلاس پایتون چیست؟

from peewee import SqliteDatabase, Model,\
    CharField, TextField, DateTimeField
from datetime import datetime

database = SqliteDatabase('Posts.db')

class Article(Model):
    title = CharField(null=True)
    body = TextField(null=True)
    url = CharField()

    created_time = DateTimeField(default=datetime.now())

    class Meta:
        db = database
فایل پیوست

Ahmad As
Ahmad As

16 بهمن 99

2
حذف شده

سلاااااااااااااااااااااام

کلاس متا در کد زیر دقیقا چه کاری رو انجام می دهد

کلاس متا تو این کد مربوط به Peewee ORM هست. قسمتی از داکیومنت Peewee:

Model configuration is kept namespaced in a special class called Meta. This convention is borrowed from Django. Meta configuration is passed on to subclasses, so our project’s models will all subclass BaseModel. There are many different attributes you can configure using Model.Meta.

(چون میدونم زبان تون خوبه، دیگه ترجمه نمیکنم. البته همه دانشجوهای این دوره به عنوان یه برنامه نویس لازمه حداقل در حد خوندن داکیومنت ها و نیاز محیط کار زبان رو یاد بگیرن)

لینک این قسمت داکیومنت:

http://docs.peewee-orm.com/en/latest/peewee/models.html#

اگر میخوای بهتر کار کلاس متا تو Peewee بفهمی لازمه بیشتر قسمت های این صفحه رو بخونی.

 

یه نکته ای که شاید لازمه باشه بگم این که class Meta تو این کلاس مفهوم پایتونی خاصی نداره. فقط یه کلاس تو یه کلاس دیگه تعرف شده که همچین کاری تو پایتون ممکنه و شما هم میتونید این کار رو انجام بدید.

 

و تفاوت آن با متاکلاس پایتون چیست؟

اگر به تعریف این ۲ توجه کنیم میفهمیم که این ۲، کاملا ۲ تا چیز جدا از هم هستن و ربطی به هم ندارن. پس بنظرم نمیشه گفت چه تفاوت یا شباهتی بهم دارن.

در مورد متاکلاس این مقاله توضیحات خوب و ابتدایی ای داده:

https://realpython.com/python-metaclasses/

قسمت خلاصه ای از این مقاله:

 

Consider the following:

>>> class Foo:
...     pass
...
>>> x = Foo()

>>> type(x)
<class '__main__.Foo'>

>>> type(Foo)
<class 'type'>

The type of x is class Foo, as you would expect. But the type of Foo, the class itself, is type. In general, the type of any new-style class is type.

The type of the built-in classes you are familiar with is also type:

>>> for t in int, float, dict, list, tuple:
...     print(type(t))
...
<class 'type'>
<class 'type'>
<class 'type'>
<class 'type'>
<class 'type'>

For that matter, the type of type is type as well (yes, really):

>>> type(type)
<class 'type'>

type is a metaclass, of which classes are instances. Just as an ordinary object is an instance of a class, any new-style class in Python, and thus any class in Python ۳, is an instance of the type metaclass.

In the above case:

x is an instance of class Foo.

Foo is an instance of the type metaclass.

type is also an instance of the type metaclass, so it is an instance of itself.

 

فایل پیوست

محمدعلی رضا

توسط

محمدعلی رضا

17 بهمن 99

جلسه نصب راه‌اندازی PEWEE ORM در پروژه