如何在默认为类级别值的 Python 方法中使用命名参数?

发布于 2024-09-15 15:08:43 字数 747 浏览 7 评论 0原文

使用场景:

# case #1 - for classes
a = MyClass() # default logger is None
a = MyClass(logger="a") # set the default logger to be "a"
a.test(logger="b") # this means that logger will be "b" only inside this method
a.test(logger=None) # this means that logger will be None but only inside this method
a.test() # here logger should defaults to the value specified when object was initialized ("a")

如何实现MyClass以便能够像上面那样使用它?

假设我在 MyClass 中有几个可以接受 logger 命名参数的方法,因此我希望有一个不需要在开始时添加大量重复代码的解决方案每个 test...() 方法。

我读到了哨兵示例,但这确实不适用于类,我不想添加全局变量来将哨兵对象保留在内部。

Usage scenario:

# case #1 - for classes
a = MyClass() # default logger is None
a = MyClass(logger="a") # set the default logger to be "a"
a.test(logger="b") # this means that logger will be "b" only inside this method
a.test(logger=None) # this means that logger will be None but only inside this method
a.test() # here logger should defaults to the value specified when object was initialized ("a")

How can I implement MyClass in order to be able to use it as above?

Let's assume that I have several methods inside MyClass that can accept the logger named parameter so I would appreciate a solution that does not require to add a lot of duplicate code at the beginning of each test...() method.

I read about the sentinel example, but this does not work for classes and I would not like to add a global variable to keep the sentinel object inside.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人事已非 2024-09-22 15:08:43
_sentinel = object()

class MyClass(object):
  def __init__(self, logger=None):
    self.logger = logger
  def test(self, logger=_sentinel):
    if logger is _sentinel: logger = self.logger

# in case you want to use this inside a function from your module use:
_sentinel = object()
logger = None
def test(logger=_sentinel)
    if logger is _sentinel: logger = globals().get('logger')

两个核心思想:捕获可能(或可能不)本地覆盖到关键字参数字典中的一组命名值;使用哨兵对象作为默认值来唯一标识某个命名参数是否已显式传递(None 通常用于此目的,但是当您想要 None 时,就像这里一样) 作为参数的“一流值”,唯一的哨兵对象也可以。

_sentinel = object()

class MyClass(object):
  def __init__(self, logger=None):
    self.logger = logger
  def test(self, logger=_sentinel):
    if logger is _sentinel: logger = self.logger

# in case you want to use this inside a function from your module use:
_sentinel = object()
logger = None
def test(logger=_sentinel)
    if logger is _sentinel: logger = globals().get('logger')

two core ideas: capturing the set of named values that may be (or may not be) locally overridden into a keywords-parameters dict; using a sentinel object as the default value to uniquely identify whether a certain named argument has been explicitly passed or not (None is often used for this purpose, but when, as here, you want None as a "first class value" for the parameter, a unique sentinel object will do just as well).

盗心人 2024-09-22 15:08:43
class MyClass(object):    
    def __init__(self, logger=None):
        self.logger = logger
    def test(self, **kwargs):
        logger = kwargs.get("logger", self.logger)
        # use logger, which is sourced as given in OP

注意

  • 使用 **kwargs 是必要的,因为您允许在 MyClass.test 中为名为变量的记录器使用 None 值。如果您选择了其他一些标记值,则可以取消此设置(但最常见的是 None)。
  • 在最初的问题中不清楚您是否想要类或实例默认值。上面给出的是一个实例默认值,即所有 MyClass 实例的默认记录器是 None,在 MyClass 构造函数中设置。
class MyClass(object):    
    def __init__(self, logger=None):
        self.logger = logger
    def test(self, **kwargs):
        logger = kwargs.get("logger", self.logger)
        # use logger, which is sourced as given in OP

Notes

  • The use of **kwargs is necessary as you're allowing None values for the logger named variable in MyClass.test. You could do away with this if you picked some other sentinel value (but None is most common).
  • It was unclear in the original question if you wanted a class or instance default. The one given above is an instance default, that is the default logger across all MyClass instances is None, set in the MyClass constructor.
最终幸福 2024-09-22 15:08:43
class MyClass(object):
    def __init__(self, logger = None):
        self.logger = logger

    def test(self, **kwargs):
        logger = self.logger            
        if kwargs.has_key("logger"):
            logger = kwargs.get(logger)
        print "logger is %s" logger.name

简要说明:test 首先假设它将使用实例记录器。但是,如果显式记录器作为命令行参数传入,它将被使用。如果 logger = None 作为关键字参数传递,则不使用记录器。

class MyClass(object):
    def __init__(self, logger = None):
        self.logger = logger

    def test(self, **kwargs):
        logger = self.logger            
        if kwargs.has_key("logger"):
            logger = kwargs.get(logger)
        print "logger is %s" logger.name

Brief explanation: test starts off by assuming that it is going to use the instance logger. However if an explicit logger is passed in as command line argument it will be used instead. If logger = None is passed as a key word argument then no logger is used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文