在python中使用setattr() [英] Using setattr() in python

查看:126
本文介绍了在python中使用setattr()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找可以解释使用方法基础知识的人,而不是使用setattr().

I am looking for someone to explain the basics of how to use, and not use setattr().

我的问题是尝试使用一个类的方法/函数返回数据,然后将其放入另一方法/函数中.在这种情况下,也许一种更简单的方法会更好,但是我试图了解类是如何工作/使用的.这个问题似乎取决于setattr(),这是我试图对此做一个相当简单的尝试.

My problem arose trying to use one class method/function to return data that is then put in another method/function. Perhaps a simpler approach would be much better in this case, but I'm trying to understand how classes work/are used. This problem seems to hinge on setattr(), and this is my attempt to make a fairly simple use of this.

尽管问题不完全相同,但我遵循的是 Python的艰难之路,例如ex42 &mdash ; while循环@第18-41行.

Though it's not quite the same problem, I was following Python The Hard Way, ex42—the while loop @ lines 18-41.

我尝试编写\__init__(),并改用getattr(),认为可能需要在类的命名空间中添加某些内容,但这似乎无济于事.

I tried writing an \__init__(), and using getattr() instead, thinking perhaps something needed to be in the class' namespace, but this doesn't seem to help.

#! /bin/python2.6

class HolyGrail(object):

    def __init__(self):
        self.start = 'start_at_init'

    # function definition in question:
    # TypeError: 'str' object is not callable

    def run_it(self):
        start = setattr(self, 'name', 'get_thing')
        start = self.name

        # Something wrong here?
        value_returned = start() #I believe this == self.get_thing()
        use_it(value_returned)

    """
    # alternate function definitions
    # NameError: global name 'start' is not defined

    def __init__(self):
        self.start = 'get_thing'

    def run_it(self):
        go_do_it = getattr(self, start)
        first_output = go_do_it()
        use_it(first_output)
    """

    def get_thing(self):
        return "The Knights Who Say ... Ni!"

    def use_it(self, x):
        print x
        print "We want a shrubbery!"

my_instance = HolyGrail()
my_instance.run_it()


@Karl Knechtel,@ Amber和@Chris Morgan感谢您的帮助.


@Karl Knechtel, @Amber , @Chris Morgan thanks for your help.

我想我现在可以解释我自己的答案了!这需要更好地把握自我,成为我的目标.这是一个实例名称,上面标记着诸如属性之类的东西.

I think I can now explain my own answer! This required a better grasp of self as an object for me. It's an instance name that gets tagged up with stuff like attributes.

该班级可能是城镇,然后是. getattr使用它的名称查找房子,因此您可以随时调用它,如果找不到房子,它会提出一个不同的地方 -使用getattr,存在一个名称",然后您就可以找到它.动态地从一个功能过渡到另一个功能 作为奖励,您可能具有默认值,对于获取后备默认方法很有用-连接失败或什么原因?

The class could be a Town, and then. getattr looks for a house using it's name so you are ready to call on it soon, and comes up with a different place if you don't find the house --With getattr a 'name' exists, and you go find it. Makes the step from one function to another dynamic As a bonus you may have a default value, useful to get a fallback default method--connection failed or something?

setattr盖房子并为其命名,以便稍后使用. 您可能会重建这座房子,或者在找不到的地方去某个地方. -setattr设置属性名称并给出或更改其值,以供稍后调用 也许用户关闭了声音,那么以后的方法将不会输出任何音频.

setattr builds a house and gives it a name so you can call in on it later. You could potentially rebuild this house, or go to a particular place if you are unable to find it. --setattr makes an attribute name and gives, or changes it's value, to be called on later Perhaps a user turns sound off, then future methods don't output any audio.

我可以用多种方法编写函数,但是无需更改任何属性:

I could have written my function a number of ways, but there's no need to change any attributes:

def run_it(self):
    yo = getattr(self, 'get_thing')
    answer = yo()
    setattr(self, 'deal_accepted', self.use_it) #really ott
    no = getattr(self, 'deal_accepted')
    no(answer)

正确更正的代码:

def run_it(self):
    value_returned = self.get_thing()
    self.use_it(value_returned)

推荐答案

您正在将self.name设置为 string "get_thing",而不是函数get_thing.

You are setting self.name to the string "get_thing", not the function get_thing.

如果希望self.name是一个函数,则应将其设置为一个:

If you want self.name to be a function, then you should set it to one:

setattr(self, 'name', self.get_thing)

但是,对于您的其他代码,这是完全不必要的,因为您可以直接调用它:

However, that's completely unnecessary for your other code, because you could just call it directly:

value_returned = self.get_thing()

这篇关于在python中使用setattr()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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