内部类方法是否返回值或只是修改python中的实例变量? [英] Should internal class methods returnvalues or just modify instance variables in python?

查看:104
本文介绍了内部类方法是否返回值或只是修改python中的实例变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个查询构建器类,它将帮助从URL参数构建mongodb的查询。除了使用基本的语言结构和使用django内置的模型之外,我从来没有做过很多面向对象的编程,还是设计了除了自己以外的人的消费类。

I am creating a query builder class that will help in constructing a query for mongodb from URL params. I have never done much object oriented programming, or designed classes for consumption by people other than myself, besides using basic language constructs and using django's built in Models.

所以我有这个 QueryBuilder class

So I have this QueryBuilder class

class QueryHelper():
    """
    Help abstract out the problem of querying over vastly
    different dataschemas.
    """

    def __init__(self, collection_name, field_name, params_dict):
        self.query_dict = {}
        self.params_dict = params_dict
        db = connection.get_db()
        self.collection = db[collection_name]

    def _build_query(self):
        # check params dict and build a mongo query
        pass

现在在 _build_query 我将检查参数dict并填充 query_dict ,以便将其传递给mongo的 find()函数。
这样做我只是想知道是否有一个绝对正确的方法,因为 _build_query 应该返回一个字典,还是应该修改 self.query_dict 。因为它是一个内部方法,我认为只要修改 self.query_dict 就可以了。有没有正确的方法(pythonic)接近这个方法?这是否只是愚蠢而不是重要的设计决定?任何帮助是赞赏。谢谢。

Now in _build_query i will be checking the params dict and populating query_dict so as to pass it to mongo's find() function. In doing this I was just wondering if there was an absolute correct approach to as whether _build_query should return a dictionary or whether it should just modify self.query_dict. Since it is an internal method I would assume it is ok to just modify self.query_dict. Is there a right way (pythonic) way of approaching this?? Is this just silly and not an important design decision?? Any help is appreciated. Thank you.

推荐答案

返回一个值是可取的,因为它允许你将所有的属性修改在一个地方( __初始化__ )。此外,这使得稍后可以更容易地扩展代码;假设你想覆盖一个子类中的 _build_query ,那么覆盖方法只能返回一个值,而不需要知道要设置哪个属性。以下是一个例子:

Returning a value is preferable as it allows you to keep all the attribute modifying in one place (__init__). Also, this makes it easier to extend the code later; suppose you want to override _build_query in a subclass, then the overriding method can just return a value, without needing to know which attribute to set. Here's an example:

class QueryHelper(object):
    def __init__(self, param, text):
        self._param = param
        self._query = self._build_query(text)

    def _build_query(self, text):
        return text + " and ham!"

class RefinedQueryHelper(QueryHelper):
    def _build_query(self, text):
        # no need to know how the query object is going to be used
        q = super(RefinedQueryHelper, self)._build_query()
        return q.replace("ham", "spam")

vs。 setter version:

vs. the "setter version":

class QueryHelper(object):
    def __init__(self, param, text):
        self._param = param
        self._build_query(text)

    def _build_query(self, text):
        self._query = text + " and ham!"

class RefinedQueryHelper(QueryHelper):
    def _build_query(self, text):
        # what if we want to store the query in __query instead?
        # then we need to modify two classes...
        super(RefinedQueryHelper, self)._build_query()
        self._query = self._query.replace("ham", "spam")

如果您选择设置属性,则可能需要调用方法 _set_query 为了清楚起见。

If you do choose to set an attribute, you might want to call the method _set_query for clarity.

这篇关于内部类方法是否返回值或只是修改python中的实例变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆