English 中文(简体)
要求将属性转换为字符串的Python
原标题:Python requiring an attribute be converted to a string
from sys import exit
from random import randint

class Map(object):

 def death(): 
    print quips[randint (0, len(quips)-1)]
    exit(1)

 def princess_lives_here():
    print "You see a beautiful Princess with a shiny crown."
    print "She offers you some cake."

    eat_it = raw_input(">")

    if eat_it == "eat it":
        print "You explode like a pinata full of frogs."
        print "The Princess cackles and eats the frogs. Yum!"
        return  death 

    elif eat_it == "do not eat it":
        print "She throws the cake at you and it cuts off your head."
        print "The last thing you see is her munching on your face. Yum!"
        return  death 

    elif eat_it == "make her eat it":
        print "The Princess screams as you cram the cake in her mouth."
        print "Then she smiles and cries and thank you for saving her."
        print "She points to a tiny door and says,  The Koi needs cake too. "
        print "She gives you the very last bit of cake and shoves you in."
        return  gold_koi_pond 

    else:
        print "The Princess looks at you confused and just points at the cake."
        return  princess_lives_here 

class Engine(object):

 def __init__(self, start, quips):
    self.quips = [
        "You died. You suck at this.",
        "Your mom would be proud, if she were smarter",
        "Such a luser.",
        "I have a small puppy that s better at this."
    ]
    self.start = start

 def play(self):
    next = self.start

    while True:
        print "
-----"
        room = getattr(self, next)
        next = room()

m = Map()
e = Engine(m, "princess_lives_here")

e.play()

我在终端得到的追溯是:

    Traceback (most recent call last):
  File "ec42.py", line 162, in <module>
    e.play()
  File "ec42.py", line 156, in play
    room = getattr(self, next)
TypeError: getattr(): attribute name must be string

我做这件事太久了,就是无法确定。主要问题是让map类作为对象在引擎类中运行。提前感谢您的帮助。

最佳回答

也许你想要这样的东西?

class Map(object):

 def __init__(self):

    self.quips = [
        "You died. You suck at this.",
        "Your mom would be proud, if she were smarter",
        "Such a luser.",
        "I have a small puppy that s better at this."
    ]

 def death(self): 
    print self.quips[randint (0, len(self.quips)-1)]
    exit(1)

 def princess_lives_here(self):
    print "You see a beautiful Princess with a shiny crown."
    print "She offers you some cake."

    eat_it = raw_input(">")

    if eat_it == "eat it":
        print "You explode like a pinata full of frogs."
        print "The Princess cackles and eats the frogs. Yum!"
        return  death 

    elif eat_it == "do not eat it":
        print "She throws the cake at you and it cuts off your head."
        print "The last thing you see is her munching on your face. Yum!"
        return  death 

    elif eat_it == "make her eat it":
        print "The Princess screams as you cram the cake in her mouth."
        print "Then she smiles and cries and thank you for saving her."
        print "She points to a tiny door and says,  The Koi needs cake too. "
        print "She gives you the very last bit of cake and shoves you in."
        return  gold_koi_pond 

    else:
        print "The Princess looks at you confused and just points at the cake."
        return  princess_lives_here 

class Engine(object):

 def __init__(self, map, start):
    self.quips = [
        "You died. You suck at this.",
        "Your mom would be proud, if she were smarter",
        "Such a luser.",
        "I have a small puppy that s better at this."
    ]
    self.map = map
    self.start = start

 def play(self):
    next = self.start

    while True:
        print "
-----"
        room = getattr(self.map, next)
    next = room()
问题回答
def __init__(self, start, quips):

。。。

e = Engine(m, "princess_lives_here")

这是你的问题。第二行使用参数m和“princess_lives_here”调用init。第一个参数应该是“princess_lives_here”,第二个应该是“quips”列表。

若要返回对象的命名属性的值,必须提供属性名称的字符串。

room = getattr(self,  next )

From the python docs: getattr(object, name[, default])

返回对象的命名属性的值。名称必须是字符串。如果字符串是对象某个属性的名称,那么结果就是该属性的值。例如,getattr(x,foobar)相当于x.foobar。如果命名属性不存在,则在提供时返回默认值,否则将引发AttributeError。

self.start是Map的一个实例,但getattr()的第二个参数必须是字符串,即属性的名称。

此外,实例方法应该将“self”作为第一个参数。

错误消息实际上非常清楚地告诉了您这里的错误:-)

room = getattr(self, next)

应该沿着

room = getattr(self,  next )

Python文档

但这只是问题的一部分。如果我猜对了您在代码示例中遗漏的内容,您可能还需要向映射对象添加__call__方法。否则room=getattr(self,next)就没有那么大意义了:-)好吧,还有这里可能缺少的其他代码;-)

此外,Map类上的所有方法定义都缺少“self”参数。





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签