OOP PYTHON:使用 cls() 创建多个构造函数而不调用 __init__

OOP PYTHON: Using cls() to create multiple constructor without calling __init__

我有一个 Python class,它接受一个 url 参数并在新闻网站上启动一个爬虫。

对象创建完成后,对象将存储在 Elasticsearch 集群中。

我想创建一个方法来接收 Elasticsearch 文档的输入,并从中创建一个对象。

class NewsArticle():

    def __init__(self, url):
        self.url = url
        # Launch a crawler and fill in the other fields like author, date, ect ...

    @classmethod
    def from_elasticsearch(cls, elasticsearch_article):
        document = elasticsearch_article['_source']
        obj = cls(document['url'])
        obj.url = document['url']
        obj.author = document['author']
        .
        .
        .

问题是,当我打电话...

# response is my document from elasticsearch
res = NewsArticle.from_elasticsearch(response)

...方法 __init__ 将被调用并启动我的爬虫。无论如何,它不会启动我的爬虫或调用 init 方法吗?

简单的 if 和默认参数 crawl 怎么样:

class NewsArticle():

    def __init__(self, url, crawl=True):
        self.url = url
        if crawl:
            # Launch a crawler and fill in the other fields like author, date, ect ...

    @classmethod
    def from_elasticsearch(cls, elasticsearch_article):
        document = elasticsearch_article['_source']
        obj = cls(document['url'], crawl=False)
        obj.url = document['url']
        obj.author = document['author']