我有一个类如下:
class User:
def __init__(self):
self.data = []
self.other_data = []
def doSomething(self, source):
// if source = 'other_data' how to access self.other_data
Run Code Online (Sandbox Code Playgroud)
我想传入源变量的字符串doSomething并访问同名的类成员.
我已经尝试过getattr只能在函数上工作(从我能说的)以及User扩展dict和使用self.__getitem__,但这也不起作用.做这个的最好方式是什么?
如果我在字符串中有对象和方法名称,我该如何调用该方法?
class Foo:
def bar1(self):
print 1
def bar2(self):
print 2
def callMethod(o, name):
???
f = Foo()
callMethod(f, "bar1")
Run Code Online (Sandbox Code Playgroud) 为简单起见,这是我想要做的精简版:
def foo(a):
# I want to print the value of the variable
# the name of which is contained in a
Run Code Online (Sandbox Code Playgroud)
我知道如何在PHP中执行此操作:
function foo($a) {
echo $$a;
}
global $string = "blah"; // might not need to be global but that's irrelevant
foo("string"); // prints "blah"
Run Code Online (Sandbox Code Playgroud)
有什么办法吗?
如何引用this_prize.left或this_prize.right使用变量?
from collections import namedtuple
import random
Prize = namedtuple("Prize", ["left", "right"])
this_prize = Prize("FirstPrize", "SecondPrize")
if random.random() > .5:
choice = "left"
else:
choice = "right"
# retrieve the value of "left" or "right" depending on the choice
print("You won", this_prize.choice)
AttributeError: 'Prize' object has no attribute 'choice'
Run Code Online (Sandbox Code Playgroud) 我怎样才能完成这样的工作:
def get_foo(someobject, foostring):
return someobject.foostring
Run Code Online (Sandbox Code Playgroud)
IE:
如果我这样做get_foo(obj, "name")应该是调用obj.name(请参阅输入字符串,但我称之为attritube.
谢谢
简单的问题,但由于我是python的新手,从php转发,我得到了一些错误.
我有以下简单的类:
User(object)
fullName = "John Doe"
user = User()
Run Code Online (Sandbox Code Playgroud)
在PHP中,我可以执行以下操作:
$param = 'fullName';
echo $user->$param; // return John Doe
Run Code Online (Sandbox Code Playgroud)
我怎么在python中这样做?
我有一个Python类,其属性名为:date1,date2,date3等.
在运行时,我有一个变量i,它是一个整数.
我想要做的是根据i的值在运行时访问适当的日期属性.
例如,
如果我== 1,我想访问myobject.date1
如果我== 2,我想访问myobject.date2
我想为类而不是属性做类似的事情.
例如,我有一堆类:MyClass1,MyClass2,MyClass3等.我有一个变量k.
如果k == 1,我想实例化一个新的MyClass1实例
如果k == 2,我想实例化一个新的MyClass2实例
我怎样才能做到这一点?
编辑
我希望避免使用巨大的if-then-else语句来选择合适的属性/类.
有没有办法在Python中使用变量的值动态编写类名?
我有一个伪或特殊属性的对象,可以用三种不同的方式命名(注意:我不控制生成对象的代码)
属性中的值(取决于哪一个)是完全相同的,我需要得到它以进行进一步处理,因此根据数据源,我可以有类似的东西:
>>> obj.a
'value'
>>> obj.b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: Obj instance has no attribute 'b'
>>> obj.c
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: Obj instance has no attribute 'c'
Run Code Online (Sandbox Code Playgroud)
要么
>>> obj.a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: Obj instance has no attribute 'a'
>>> obj.b
'value'
>>> obj.c
Traceback (most recent call last): …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用函数返回给定列表.
def get_ext(file_type):
text = ['txt', 'doc']
audio = ['mp3', 'wav']
video = ['mp4', 'mkv']
return ?????
get_ext('audio') #should return de list ['mp3', 'wav']
Run Code Online (Sandbox Code Playgroud)
然后我被卡住了.这是一个扩展列表的简单/简短示例.最简单的方法是什么?
我正在用 Python 编写棋盘游戏《大富翁》。《大富翁》拥有三种类型的土地供玩家购买:房产(如木板路)、铁路和公用事业。房产有 6 种条件(0-4 栋房屋或酒店)的可变购买价格和租金。铁路和公用事业有固定的价格和租金,具体取决于您拥有的其他铁路或公用事业的数量。
我有一个 Game() 类,其中包含三个字典属性,其所有键都是地块在棋盘上的位置(从 0 到 39):
我这样做是因为在某些时候我想迭代相应的字典来查看玩家是否拥有该字典中的其他土地;并且还因为值的数量不同。
Game() 还有一个名为 space_types 的元组,其中每个值都是代表空间类型(财产、铁路、公用事业、奢侈税、GO 等)的数字。要找出我的玩家坐在哪种 space_type 上:
space_type = space_types[boardposition]
我还有一个带有 buy_property() 方法的 Player() 类,其中包含一条打印语句,该语句应该显示:
"You bought PropertyName for $400."
其中 PropertyName 是空间的名称。但现在我必须像这样使用 if/elif/else 块,这看起来很难看:
space_type = Game(space_types[board_position])
if space_type is "property":
# pull PropertyName from Game.properties
elif space_type is "railroad":
# pull PropertyName from Game.railroads
elif space_type is "utility":
# pull PropertyName from Game.utilities
else:
# error, something weird has happened
Run Code Online (Sandbox Code Playgroud)
我想做的是这样的:
dictname …Run Code Online (Sandbox Code Playgroud)