python setattr无限递归,在python中使用setattr()

本文探讨了Python中类和对象的基础,特别是如何使用setattr()和getattr()。作者在尝试使用一个类方法返回数据并将其用于另一个方法时遇到问题。通过示例代码,解释了setattr()用于动态设置对象属性,而getattr()用于获取属性。文章指出,直接调用方法而非使用这两个函数可能是更简单的做法,并提供了修正后的代码。最后,作者总结了这两个函数在类中的作用及其重要性。
摘要由CSDN通过智能技术生成

I am looking for someone to explain the basics of how to use, and not use 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.

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

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 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.

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 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)

Properly corrected code:

def run_it(self):

value_returned = self.get_thing()

self.use_it(value_returned)

解决方案

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

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值