Home >Backend Development >Python Tutorial >What are Python metaclasses? Introduction to Python metaclasses

What are Python metaclasses? Introduction to Python metaclasses

不言
不言Original
2018-09-12 15:09:431628browse

This article brings you what is a Python metaclass? The introduction to Python metaclasses has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

The most difficult knowledge point to master in python in two sentences - metaclasses

Don't be fooled by the so-called "metaclasses are a feature that 99% of python programmers will not use" Such rhetoric scares me. Because Every Chinese is a natural user of metaclasses

To learn about metaclasses, you only need to know two sentences:

  • 道生一,一生二,二生三,三三生五世

  • Who am I? Where do I come from? Where am I going? (The blogger thinks it should be: Who am I? Where do I come from? What can I do?)

In the python world, there is an eternal Tao, That is "type", please keep it in mind, type is Tao. Such a vast python ecosystem is all generated by type.

What are Python metaclasses? Introduction to Python metaclasses

Tao creates one, creates two, two creates three, and three creates all things.

  1. is type

  2. ##一 is metaclass (metaclass, or Class generator)

  3. is class (class, or instance generator)

  4. Three is instance(instance)

  5. everything is the various attributes and methods of the instance. When we usually use python, we call them.

Tao and one are the propositions we discuss today, while two, three, and all things are the classes, instances, attributes and methods we often use. Let’s use hello world as an example. :

# 创建一个Hello类,拥有属性say_hello ----二的起源
class Hello():
    def say_hello(self, name='world'):
        print('Hello, %s.' % name)


# 从Hello类创建一个实例hello ----二生三
hello = Hello()

# 使用hello调用方法say_hello ----三生万物
hello.say_hello()

Output effect:

Hello, world.

This is a standard process of "two begets three, three begets all things". From the class to the methods we can call, these two steps are used.

Then we can’t help but ask, where do classes come from? Go back to the first line of code.

class Hello is actually the "semantic abbreviation" of a function, just to make the code easier to understand. Another way of writing it is:

def fn(self, name='world'): # 假如我们有一个函数叫fn
    print('Hello, %s.' % name)
    
Hello = type('Hello', (object,), dict(say_hello=fn)) # 通过type创建Hello class ---- 神秘的“道”,可以点化一切,这次我们直接从“道”生出了“二”

This way of writing is just like the previous way of writing Class Hello. The effect is exactly the same, you can try to create an instance and call

# 从Hello类创建一个实例hello ----二生三,完全一样
hello = Hello()
# 使用hello调用方法say_hello ----三生万物,完全一样
hello.say_hello()

The output effect:

Hello, world. ----调用结果完全一样。

Let’s look back at the most exciting part,

Road directly gave birth to two:

Hello = type('Hello', (object,), dict(say_hello=fn))

This is the "Tao", the origin of the python world, you can be amazed by this.

Pay attention to its three parameters! It coincides with the three eternal propositions of mankind: who am I, where do I come from, and where do I want to go.

  • The first parameter: Who am I. Here, I need a name that is different from everything else. The above example names me "Hello"

  • Second parameter: Where do I come from

    Here, I need to know where it comes from, which is my "parent class". In the above example, my parent class is "object" - a very junior class in python.

  • The third parameter: where do I want to go

    Here, we include the methods and attributes that need to be called into a dictionary, and then pass them in as parameters. In the above example, we have a say_hello method wrapped in a dictionary.

It is worth noting that the three eternal propositions are possessed by all classes, all instances, and even all instance attributes and methods. It stands to reason that their "creators", Tao and Yi, that is, type and metaclass, also have these three parameters. But usually, the three eternal propositions of the class are not passed in as parameters, but are passed in as follows

class Hello(object){
# class 后声明“我是谁”
# 小括号内声明“我来自哪里”
# 中括号内声明“我要到哪里去”
    def say_hello(){
        
    }
}

  • Creator can directly create a single person, but this is a Hard labor. The Creator will first create the species "human" and then create specific individuals in batches. And pass on the three eternal propositions.

  • "Tao" can directly produce "two", but it will first produce "one" and then produce "two" in batches.

  • type can directly generate a class (class), but it can also generate a metaclass (metaclass) first, and then use the metaclass to batch customize the class (class).

Metaclass - Tao Sheng Yi, life two

Generally speaking, metaclasses are named with the suffix Metalass. Imagine that we need a metaclass that can automatically say hello. As for the class methods in it, sometimes we need say_Hello, sometimes we need say_Hi, sometimes we need say_Sayolala, and sometimes we need say_Nihao.

If each built-in say_xxx needs to be declared once in the class, what a terrible hard work it will be! It is better to use

metaclass to solve the problem.

The following is the code to create a metaclass specifically for "saying hello":

class SayMetaClass(type):

    def __new__(cls, name, bases, attrs):
        attrs['say_'+name] = lambda self,value,saying=name: print(saying+','+value+'!')
        return type.__new__(cls, name, bases, attrs)

Remember two points:

1. The metaclass is derived from "type", so the parent Classes need to pass in type. 【
Tao generates one, so one must contain Tao

2、元类的操作都在 __new__中完成,它的第一个参数是将创建的类,之后的参数即是三大永恒命题:我是谁,我从哪里来,我将到哪里去。 它返回的对象也是三大永恒命题,接下来,这三个参数将一直陪伴我们。

在__new__中,我只进行了一个操作,就是

attrs['say_'+name] = lambda self,value,saying=name: print(saying+','+value+'!')

它跟据类的名字,创建了一个类方法。比如我们由元类创建的类叫“Hello”,那创建时就自动有了一个叫“say_Hello”的类方法,然后又将类的名字“Hello”作为默认参数saying,传到了方法里面。然后把hello方法调用时的传参作为value传进去,最终打印出来。

那么,一个元类是怎么从创建到调用的呢?
来!一起根据道生一、一生二、二生三、三生万物的准则,走进元类的生命周期吧!

# 道生一:传入type
class SayMetaClass(type):

    # 传入三大永恒命题:类名称、父类、属性
    def __new__(cls, name, bases, attrs):
        # 创造“天赋”
        attrs['say_'+name] = lambda self,value,saying=name: print(saying+','+value+'!')
        # 传承三大永恒命题:类名称、父类、属性
        return type.__new__(cls, name, bases, attrs)

# 一生二:创建类
class Hello(object, metaclass=SayMetaClass):
    pass

# 二生三:创建实列
hello = Hello()

# 三生万物:调用实例方法
hello.say_Hello('world!')

输出为

Hello, world!

注意:通过元类创建的类,第一个参数是父类,第二个参数是metaclass

普通人出生都不会说话,但有的人出生就会打招呼说“Hello”,“你好”,“sayolala”,这就是天赋的力量。它会给我们面向对象的编程省下无数的麻烦。

现在,保持元类不变,我们还可以继续创建Sayolala, Nihao类,如下:

# 一生二:创建类
class Sayolala(object, metaclass=SayMetaClass):
    pass

# 二生三:创建实列
s = Sayolala()

# 三生万物:调用实例方法
s.say_Sayolala('japan!')

输出

Sayolala, japan!

也可以说中文

# 一生二:创建类
class Nihao(object, metaclass=SayMetaClass):
    pass

# 二生三:创建实列
n = Nihao()

# 三生万物:调用实例方法
n.say_Nihao('中华!')

输出

Nihao, 中华!

再来一个小例子:

# 道生一
class ListMetaclass(type):
    def __new__(cls, name, bases, attrs):
        # 天赋:通过add方法将值绑定
        attrs['add'] = lambda self, value: self.append(value)
        return type.__new__(cls, name, bases, attrs)
        
# 一生二
class MyList(list, metaclass=ListMetaclass):
    pass
    
# 二生三
L = MyList()

# 三生万物
L.add(1)

现在我们打印一下L

print(L)

>>> [1]

而普通的list没有add()方法

L2 = list()
L2.add(1)

>>>AttributeError: 'list' object has no attribute 'add'

太棒了!学到这里,你是不是已经体验到了造物主的乐趣?

年轻的造物主,请随我一起开创新世界。

我们选择两个领域,一个是Django的核心思想,“Object Relational Mapping”,即对象-关系映射,简称ORM。

这是Django的一大难点,但学完了元类,一切变得清晰。你对Django的理解将更上一层楼!

另一个领域是爬虫领域(黑客领域),一个自动搜索网络上的可用代理,然后换着IP去突破别的人反爬虫限制。

这两项技能非常有用,也非常好玩!

挑战一:通过元类创建ORM

准备工作,创建一个Field类

class Field(object):

    def __init__(self, name, column_type):
        self.name = name
        self.column_type = column_type

    def __str__(self):
        return &#39;<%s:%s>&#39; % (self.__class__.__name__, self.name)

它的作用是
在Field类实例化时将得到两个参数,name和column_type,它们将被绑定为Field的私有属性,如果要将Field转化为字符串时,将返回“Field:XXX” , XXX是传入的name名称。

准备工作:创建StringField和IntergerField

class StringField(Field):

    def __init__(self, name):
        super(StringField, self).__init__(name, &#39;varchar(100)&#39;)

class IntegerField(Field):

    def __init__(self, name):
        super(IntegerField, self).__init__(name, &#39;bigint&#39;)

它的作用是
在StringField,IntegerField实例初始化时,时自动调用父类的初始化方式。

道生一

class ModelMetaclass(type):

    def __new__(cls, name, bases, attrs):
        if name==&#39;Model&#39;:
            return type.__new__(cls, name, bases, attrs)
        print(&#39;Found model: %s&#39; % name)
        mappings = dict()
        for k, v in attrs.items():
            if isinstance(v, Field):
                print(&#39;Found mapping: %s ==> %s&#39; % (k, v))
                mappings[k] = v
        for k in mappings.keys():
            attrs.pop(k)
        attrs[&#39;__mappings__&#39;] = mappings # 保存属性和列的映射关系
        attrs[&#39;__table__&#39;] = name # 假设表名和类名一致
        return type.__new__(cls, name, bases, attrs)

它做了以下几件事

  1. 创建一个新的字典mapping

  2. 将每一个类的属性,通过.items()遍历其键值对。如果值是Field类,则打印键值,并将这一对键值绑定到mapping字典上。

  3. 将刚刚传入值为Field类的属性删除。

  4. 创建一个专门的__mappings__属性,保存字典mapping。

  5. 创建一个专门的__table__属性,保存传入的类的名称。

一生二

class Model(dict, metaclass=ModelMetaclass):

    def __init__(self, **kwarg):
        super(Model, self).__init__(**kwarg)

    def __getattr__(self, key):
        try:
            return self[key]
        except KeyError:
            raise AttributeError("&#39;Model&#39; object has no attribute &#39;%s&#39;" % key)

    def __setattr__(self, key, value):
        self[key] = value

    # 模拟建表操作
    def save(self):
        fields = []
        args = []
        for k, v in self.__mappings__.items():
            fields.append(v.name)
            args.append(getattr(self, k, None))
        sql = &#39;insert into %s (%s) values (%s)&#39; % (self.__table__, &#39;,&#39;.join(fields), &#39;,&#39;.join([str(i) for i in args]))
        print(&#39;SQL: %s&#39; % sql)
        print(&#39;ARGS: %s&#39; % str(args))

如果从Model创建一个子类User:

class User(Model):
    # 定义类的属性到列的映射:
    id = IntegerField(&#39;id&#39;)
    name = StringField(&#39;username&#39;)
    email = StringField(&#39;email&#39;)
    password = StringField(&#39;password&#39;)

这时
id= IntegerField('id')就会自动解析为:

Model.__setattr__(self, 'id', IntegerField('id'))

因为IntergerField('id')是Field的子类的实例,自动触发元类的__new__,所以将IntergerField('id')存入__mappings__并删除这个键值对。

二生三、三生万物

当你初始化一个实例的时候并调用save()方法时候

u = User(id=12345, name=&#39;Batman&#39;, email=&#39;batman@nasa.org&#39;, password=&#39;iamback&#39;)
u.save()

这时先完成了二生三的过程:

  1. 先调用Model.__setattr__,将键值载入私有对象

  2. 然后调用元类的“天赋”,ModelMetaclass.__new__,将Model中的私有对象,只要是Field的实例,都自动存入u.__mappings__。

接下来完成了三生万物的过程:

通过u.save()模拟数据库存入操作。这里我们仅仅做了一下遍历__mappings__操作,虚拟了sql并打印,在现实情况下是通过输入sql语句与数据库来运行。

输出结果为

Found model: User
Found mapping: name ==> <StringField:username>
Found mapping: password ==> <StringField:password>
Found mapping: id ==> <IntegerField:id>
Found mapping: email ==> <StringField:email>
SQL: insert into User (username,password,id,email) values (Batman,iamback,12345,batman@nasa.org)
ARGS: [&#39;Batman&#39;, &#39;iamback&#39;, 12345, &#39;batman@nasa.org&#39;]
  • 年轻的造物主,你已经和我一起体验了由“道”演化“万物”的伟大历程,这也是Django中的Model版块核心原理。

  • 接下来,请和我一起进行更好玩的爬虫实战(嗯,你现在已经是初级黑客了):网络代理的爬取吧!

挑战二:网络代理的爬取

准备工作,先爬个页面玩玩

请确保已安装requests和pyquery这两个包。

# 文件:get_page.py
import requests

base_headers = {
    &#39;User-Agent&#39;: &#39;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36&#39;,
    &#39;Accept-Encoding&#39;: &#39;gzip, deflate, sdch&#39;,
    &#39;Accept-Language&#39;: &#39;zh-CN,zh;q=0.8&#39;
}


def get_page(url):
    headers = dict(base_headers)
    print(&#39;Getting&#39;, url)
    try:
        r = requests.get(url, headers=headers)
        print(&#39;Getting result&#39;, url, r.status_code)
        if r.status_code == 200:
            return r.text
    except ConnectionError:
        print(&#39;Crawling Failed&#39;, url)
        return None

这里,我们利用request包,把百度的源码爬了出来。

试一试抓百度

把这一段粘在get_page.py后面,试完删除

if(__name__ == &#39;__main__&#39;):
    rs = get_page(&#39;https://www.baidu.com&#39;)
    print(&#39;result:\r\n&#39;, rs)

试一试抓代理

把这一段粘在get_page.py后面,试完删除

if(__name__ == &#39;__main__&#39;):
    from pyquery import PyQuery as pq
    start_url = &#39;http://www.proxy360.cn/Region/China&#39;
    print(&#39;Crawling&#39;, start_url)
    html = get_page(start_url)
    if html:
        doc = pq(html)
        lines = doc(&#39;p[name="list_proxy_ip"]&#39;).items()
        for line in lines:
            ip = line.find(&#39;.tbBottomLine:nth-child(1)&#39;).text()
            port = line.find(&#39;.tbBottomLine:nth-child(2)&#39;).text()
            print(ip+&#39;:&#39;+port)

接下来进入正题:使用元类批量抓取代理

批量处理抓取代理

from getpage import get_page
from pyquery import PyQuery as pq


# 道生一:创建抽取代理的metaclass
class ProxyMetaclass(type):
    """
        元类,在FreeProxyGetter类中加入
        __CrawlFunc__和__CrawlFuncCount__
        两个参数,分别表示爬虫函数,和爬虫函数的数量。
    """
    def __new__(cls, name, bases, attrs):
        count = 0
        attrs[&#39;__CrawlFunc__&#39;] = []
        attrs[&#39;__CrawlName__&#39;] = []
        for k, v in attrs.items():
            if &#39;crawl_&#39; in k:
                attrs[&#39;__CrawlName__&#39;].append(k)
                attrs[&#39;__CrawlFunc__&#39;].append(v)
                count += 1
        for k in attrs[&#39;__CrawlName__&#39;]:
            attrs.pop(k)
        attrs[&#39;__CrawlFuncCount__&#39;] = count
        return type.__new__(cls, name, bases, attrs)


# 一生二:创建代理获取类

class ProxyGetter(object, metaclass=ProxyMetaclass):
    def get_raw_proxies(self, site):
        proxies = []
        print(&#39;Site&#39;, site)
        for func in self.__CrawlFunc__:
            if func.__name__==site:
                this_page_proxies = func(self)
                for proxy in this_page_proxies:
                    print(&#39;Getting&#39;, proxy, &#39;from&#39;, site)
                    proxies.append(proxy)
        return proxies


    def crawl_daili66(self, page_count=4):
        start_url = &#39;http://www.66ip.cn/{}.html&#39;
        urls = [start_url.format(page) for page in range(1, page_count + 1)]
        for url in urls:
            print(&#39;Crawling&#39;, url)
            html = get_page(url)
            if html:
                doc = pq(html)
                trs = doc(&#39;.containerbox table tr:gt(0)&#39;).items()
                for tr in trs:
                    ip = tr.find(&#39;td:nth-child(1)&#39;).text()
                    port = tr.find(&#39;td:nth-child(2)&#39;).text()
                    yield &#39;:&#39;.join([ip, port])

    def crawl_proxy360(self):
        start_url = &#39;http://www.proxy360.cn/Region/China&#39;
        print(&#39;Crawling&#39;, start_url)
        html = get_page(start_url)
        if html:
            doc = pq(html)
            lines = doc(&#39;p[name="list_proxy_ip"]&#39;).items()
            for line in lines:
                ip = line.find(&#39;.tbBottomLine:nth-child(1)&#39;).text()
                port = line.find(&#39;.tbBottomLine:nth-child(2)&#39;).text()
                yield &#39;:&#39;.join([ip, port])

    def crawl_goubanjia(self):
        start_url = &#39;http://www.goubanjia.com/free/gngn/index.shtml&#39;
        html = get_page(start_url)
        if html:
            doc = pq(html)
            tds = doc(&#39;td.ip&#39;).items()
            for td in tds:
                td.find(&#39;p&#39;).remove()
                yield td.text().replace(&#39; &#39;, &#39;&#39;)


if __name__ == &#39;__main__&#39;:
    # 二生三:实例化ProxyGetter
    crawler = ProxyGetter()
    print(crawler.__CrawlName__)
    # 三生万物
    for site_label in range(crawler.__CrawlFuncCount__):
        site = crawler.__CrawlName__[site_label]
        myProxies = crawler.get_raw_proxies(site)

道生一:元类的__new__中,做了四件事:

  1. 将“crawl_”开头的类方法的名称推入ProxyGetter.__CrawlName__

  2. 将“crawl_”开头的类方法的本身推入ProxyGetter.__CrawlFunc_

  3. 计算符合“crawl_”开头的类方法个数

  4. 删除所有符合“crawl_”开头的类方法

怎么样?是不是和之前创建ORM的__mappings__过程极为相似?

一生二:类里面定义了使用pyquery抓取页面元素的方法

分别从三个免费代理网站抓取了页面上显示的全部代理。

如果对yield用法不熟悉,可以查看:
廖雪峰的python教程:生成器

二生三:创建实例对象crawler

三生万物:遍历每一个__CrawlFunc__

  1. 在ProxyGetter.__CrawlName__上面,获取可以抓取的的网址名。

  2. 触发类方法ProxyGetter.get_raw_proxies(site)

  3. 遍历ProxyGetter.__CrawlFunc__,如果方法名和网址名称相同的,则执行这一个方法

  4. 把每个网址获取到的代理整合成数组输出。

那么。。。怎么利用批量代理,冲击别人的网站,套取别人的密码,狂发广告水贴,定时骚扰客户? 呃!想啥呢!这些自己悟!如果悟不到,请听下回分解!

年轻的造物主,创造世界的工具已经在你手上,请你将它的威力发挥到极致!

请记住挥动工具的口诀:

  • 道生一,一生二,二生三,三生万物

  • 我是谁,我来自哪里,我要到哪里去

相关推荐:

python数据类型元组详细介绍

Python 元类实例解析_python

The above is the detailed content of What are Python metaclasses? Introduction to Python metaclasses. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more