制作可在 lambda 中使用的属性的最 Pythonic 方法是什么?

发布于 2024-11-26 05:52:46 字数 2280 浏览 1 评论 0原文

更具体地说,我希望能够支持 lambda:,但我希望保持代码清晰简洁。我必须验证该值,因此我需要某种类型的设置器。我需要使用 lambda,因为我需要将回调传递给 Tkinter 事件。我还需要能够修改绑定外部的属性值。

在下面的示例中,假设已全局声明了一个名为 spam_button 的按钮小部件。还假设 Eggs 类将至少有 10 个属性,并且需要以相同的方式访问所有属性(我喜欢一致性)。

我可以做到这一点的第一种可能的方法是仅使用 getter 和 setter:

class Eggs(object):

    def __init__(self):
        self._spam = ''
        self.set_spam('Monster')
        print self.get_spam()
        spam_button.bind('<Enter>', lambda: self.set_spam('Ouch'))

    def set_spam(self, spam):
        if len(spam) <= 32:
            self._spam = spam

    def get_spam(self):
        return self._spam

但是如果我使用此方法并且有很多属性,则代码可能会太长而难以管理。

第二种方法是使用属性,并在回调中使用 setter:

class Eggs(object):

    def __init__(self):
        self._spam = ''
        self.spam = 'Monster'
        print self.spam
        spam_button.bind('<Enter>', lambda: self.set_spam('Ouch'))

    def set_spam(self, spam):
        if len(spam) <= 32:
            self._spam = spam

    def get_spam(self):
        return self._spam

    spam = property(get_spam, set_spam)

这种方法在直接访问属性时比第一种方法简单一些,但在使用 lambda 时却不一致。

第三种方法是使用 get 和 set 方法创建一个附加类:

class Spam(object):

    def __init__(self):
        self._value = ''

    def set(self, value):
        if len(spam) <= 32:
            self._value = value

    def get(self):
        return self._value


class Eggs(object):

    def __init__(self):
        self._spam = ''
        self.spam = Spam()
        self.spam.set('Monster')
        print self.spam.get()
        spam_button.bind('<Enter>', lambda: self.spam.set('Ouch'))

此方法比第一种方法更有组织性,但我需要为每种类型的验证创建一个类。

我可能做到的最后一种方法是使用方法而不是属性(属性是第二个示例):

class Eggs(object):

    def __init__(self):
        self._spam = ''
        self.spam('Monster')
        print self.spam()
        spam_button.bind('<Enter>', lambda: self.spam('Ouch'))

    def spam(self, spam=None):
        if spam != None: 
            if len(spam) <= 32:
                self._spam = spam
        else:
            return self._spam

此方法可能是最短的,但很难一眼看出我是在获取还是在设置。

这些方法中哪一个(如果有)是首选?

More specifically, I want to be able to support lambda: <some_or_other_setter>, but I want to keep the code clear and to a concise. I have to validate the value, so I need a setter of some kind. I need to use lambda because I need to pass callbacks to Tkinter events. I also need to be able to modify the value of the attribute outside a binding.

In my following examples, assume that a button widget called spam_button has been declared globally. Also asume that the class Eggs will have at least 10 attributes than need to all be accessed the same way (I like consistency).

The first possible way I could do this is using just getters and setters:

class Eggs(object):

    def __init__(self):
        self._spam = ''
        self.set_spam('Monster')
        print self.get_spam()
        spam_button.bind('<Enter>', lambda: self.set_spam('Ouch'))

    def set_spam(self, spam):
        if len(spam) <= 32:
            self._spam = spam

    def get_spam(self):
        return self._spam

But if I use this method and have lots of attributes, the code may get too long to manage.

The second way I could do this is use properties, and use the setter in the callback:

class Eggs(object):

    def __init__(self):
        self._spam = ''
        self.spam = 'Monster'
        print self.spam
        spam_button.bind('<Enter>', lambda: self.set_spam('Ouch'))

    def set_spam(self, spam):
        if len(spam) <= 32:
            self._spam = spam

    def get_spam(self):
        return self._spam

    spam = property(get_spam, set_spam)

This way is a bit simpler than the first when accessing the attribute directly, but is inconsistent when using lambda.

The third way to do this would be to make an additional class with get and set methods:

class Spam(object):

    def __init__(self):
        self._value = ''

    def set(self, value):
        if len(spam) <= 32:
            self._value = value

    def get(self):
        return self._value


class Eggs(object):

    def __init__(self):
        self._spam = ''
        self.spam = Spam()
        self.spam.set('Monster')
        print self.spam.get()
        spam_button.bind('<Enter>', lambda: self.spam.set('Ouch'))

This method is more organised than the first, but I will need to make a class for each type of validation.

The last way I might do it is use methods instead of properties (properties were the second example):

class Eggs(object):

    def __init__(self):
        self._spam = ''
        self.spam('Monster')
        print self.spam()
        spam_button.bind('<Enter>', lambda: self.spam('Ouch'))

    def spam(self, spam=None):
        if spam != None: 
            if len(spam) <= 32:
                self._spam = spam
        else:
            return self._spam

This method will probably be the shortest, but it is harder at a glance to tell whether I am getting or setting.

Which (if any) of these methods are preferred?

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

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

发布评论

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

评论(4

看春风乍起 2024-12-03 05:52:46

使用闭包返回一个用作回调的函数,并将每个条件定义为一个函数(使用lambdadef,您的偏好) 而不是将每个 setter 作为函数。

class Eggs(object):

    def __init__(self):
        self.spam = 'Monster'
        def spam_condition(string = None):
            return (string is not None) and (len(string) <= 32)
        self.spam_setter = self.set('spam', 'Ouch', spam_condition)
        spam_button.bind('<Enter>', self.spam_setter)
        self.spam_setter(val='Horse')


    def set(self, name, value, condition):
        def setter(val):
            if type(val) is not .... :  # fill this in with the TYPE of the event
                value = val
            if condition(value):
                setattr(self, name, value)
        return setter

编辑:现在您可以从任何地方调用设置器,它将检查条件。

编辑2:这样,setter 将检查它是否收到事件,如果没有,则使用它传递的值。您还可以使用:

if not isinstance(val, ....) :  # fill this in with the CLASS of the event

Use a closure to return a function to be used as a callback, and define each condition as a function (using lambda or def, your preference) instead of each setter as a function.

class Eggs(object):

    def __init__(self):
        self.spam = 'Monster'
        def spam_condition(string = None):
            return (string is not None) and (len(string) <= 32)
        self.spam_setter = self.set('spam', 'Ouch', spam_condition)
        spam_button.bind('<Enter>', self.spam_setter)
        self.spam_setter(val='Horse')


    def set(self, name, value, condition):
        def setter(val):
            if type(val) is not .... :  # fill this in with the TYPE of the event
                value = val
            if condition(value):
                setattr(self, name, value)
        return setter

Edit: Now you can call the setter, which will check the condition, from anywhere.

Edit 2: This way, the setter will check if it got an event, and if not, use the value it's passed. You could also use:

if not isinstance(val, ....) :  # fill this in with the CLASS of the event
待天淡蓝洁白时 2024-12-03 05:52:46

我仍然建议使用字典,然后就到此为止了。将其子类化并重写 __setitem__ 来进行数据验证,然后不用担心 getter:

#!/usr/bin/env python

class Egg(dict):
    def __init__(self, *args, **kwargs):
        super(Egg, self).__init__(*args, **kwargs)
        self['spam'] = 'foo'
        spam_button.bind('<Enter>', lambda: self.__setitem__('spam', 'Ouch'))

    def __setitem__(self, key, value):
        if key == 'spam':
            if len(value) > 32:
                raise ValueError('"%s" is longer than 32 characters')
            return super(Egg, self).__setitem__(key, value)
        raise KeyError(key)

I still suggest using a dict and calling it a day. Subclass it and override __setitem__ to do your data validation, then don't worry about getters:

#!/usr/bin/env python

class Egg(dict):
    def __init__(self, *args, **kwargs):
        super(Egg, self).__init__(*args, **kwargs)
        self['spam'] = 'foo'
        spam_button.bind('<Enter>', lambda: self.__setitem__('spam', 'Ouch'))

    def __setitem__(self, key, value):
        if key == 'spam':
            if len(value) > 32:
                raise ValueError('"%s" is longer than 32 characters')
            return super(Egg, self).__setitem__(key, value)
        raise KeyError(key)
请你别敷衍 2024-12-03 05:52:46

验证问题可以使用属性来处理:

class Egg(object):
    @property
    def spam(self):
        return self._spam

    @spam.setter
    def spam(self, value):    
        if len(value) <= 32:
            self._spam = value

显然,您仍然可以使用self._spam = 'spam '*10+'baked beans and spam'而不受惩罚。

使用内置的 setattr

lambda: setattr(self, 'spam', 'Ouch')

如果您反对 ..."spam"... 并且更喜欢 ...spam...,您可以使用 property 的方法:

lambda: self.__class__.spam.fset(self, 'Ouch')

或者,因为 property 是一个描述符:

lambda: type(self).spam.__set__(self, 'Ouch')

但出于明显的原因,我希望第一个版本是首选。

The validation issue can be handled using a property:

class Egg(object):
    @property
    def spam(self):
        return self._spam

    @spam.setter
    def spam(self, value):    
        if len(value) <= 32:
            self._spam = value

Obviously, you can still use self._spam = 'spam '*10+'baked beans and spam' with impunity.

Use the builtin setattr:

lambda: setattr(self, 'spam', 'Ouch')

If you object to ..."spam"... and prefer just ...spam..., you can use methods of property:

lambda: self.__class__.spam.fset(self, 'Ouch')

or, since property is a descriptor:

lambda: type(self).spam.__set__(self, 'Ouch')

But the first version is preferred, I hope for obvious reasons.

ゞ记忆︶ㄣ 2024-12-03 05:52:46

我喜欢基于类的方法。您可能需要进行一组有限的验证(例如字符串的最大长度、数字的有效范围等),因此您将拥有有限数量的类。

例如:

class LimitedLengthString(object):
    def __init__(self, max_length):
        self.max_length = max_length
    def set(self, value):
        if len(value) <= self.max_length:
            self.value = value
    def get(self):
        return value

class Eggs(object):
    def __init__(self):
        self.spam = LimitedLengthString(32)
        self.spam.set('Monster')
        print self.spam.get()
        spam_button.bind('<Enter>', lambda: self.spam.set('Ouch'))

如果您确实需要对不同的属性进行独特的验证,那么您就无法避免编写大量代码。但正如编程中一贯的那样,当您开始重复自己时,请进行概括。


TokenMacGuy 建议后更新:使用相当未知的 Python 功能“描述符”的替代方案:

class LimitedLengthString(object):
    def __init__(self, name, max_length):
        self.name = name
        self.max_length = max_length

    def __set__(self, instance, value):
        if len(value) <= self.max_length:
            instance.__dict__[self.name] = value

    def __get__(self, instance, owner):
        return instance.__dict__[self.name]

class Eggs(object):
    spam = LimitedLengthString('spam', 32)
    def __init__(self):
        self.spam = 'Monster'
        print self.spam  # prints 'Monster'
        self.spam = 'x' * 40
        print self.spam  # still 'Monster'
        spam_button.bind('<Enter>', lambda: self.spam = 'Ouch')

我发现了 对描述符非常好的介绍

I like the class-based approach. You'll probably have a limited set of validation that you want to do (for example maximum length of a string, valid range for a number, etc.), so you'll have a limited number of classes.

For example:

class LimitedLengthString(object):
    def __init__(self, max_length):
        self.max_length = max_length
    def set(self, value):
        if len(value) <= self.max_length:
            self.value = value
    def get(self):
        return value

class Eggs(object):
    def __init__(self):
        self.spam = LimitedLengthString(32)
        self.spam.set('Monster')
        print self.spam.get()
        spam_button.bind('<Enter>', lambda: self.spam.set('Ouch'))

If you really have unique validations that need to be done to the different attributes, you can't get away from writing a lot of code. But as always in programming, generalize when you start repeating yourself.


Update after suggestion by TokenMacGuy: Alternative using the fairly unknown Python feature "descriptors":

class LimitedLengthString(object):
    def __init__(self, name, max_length):
        self.name = name
        self.max_length = max_length

    def __set__(self, instance, value):
        if len(value) <= self.max_length:
            instance.__dict__[self.name] = value

    def __get__(self, instance, owner):
        return instance.__dict__[self.name]

class Eggs(object):
    spam = LimitedLengthString('spam', 32)
    def __init__(self):
        self.spam = 'Monster'
        print self.spam  # prints 'Monster'
        self.spam = 'x' * 40
        print self.spam  # still 'Monster'
        spam_button.bind('<Enter>', lambda: self.spam = 'Ouch')

I found a pretty good introduction to descriptors.

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