getattr

What is getattr() exactly and how do I use it?

大憨熊 提交于 2019-12-16 20:06:16
问题 I was reading about the getattr() function. The problem is that I still can't grasp the idea of its usage. The only thing I understand about getattr() is that getattr(li, "pop") is the same as calling li.pop . I didn't understand when the book mentioned how you use it to get a reference to a function without knowing its name until run-time. Maybe this is me being a noob in programming, in general. Could anyone shed some light to the subject? When and how do I use this exactly? 回答1: getattr

What is getattr() exactly and how do I use it?

梦想的初衷 提交于 2019-12-16 20:06:03
问题 I was reading about the getattr() function. The problem is that I still can't grasp the idea of its usage. The only thing I understand about getattr() is that getattr(li, "pop") is the same as calling li.pop . I didn't understand when the book mentioned how you use it to get a reference to a function without knowing its name until run-time. Maybe this is me being a noob in programming, in general. Could anyone shed some light to the subject? When and how do I use this exactly? 回答1: getattr

Accessing a method using getattr

随声附和 提交于 2019-12-13 21:11:22
问题 When creating a new class instance, I'm trying to call a method in a different class however can't get it to work. Here's what I have: class DataProject(object): def __init__(self, name=none,input_file=None,datamode=None,comments=None,readnow=True): .............. # here's where I'm trying to call the method in the other class if type(input_file) == str: self.input_file_format = self.input_file.split(".")[-1] if readnow: getattr(Analysis(),'read_'+self.input_file_format)(self,input_file)

Dynamically get dict elements via getattr?

浪子不回头ぞ 提交于 2019-12-12 11:01:24
问题 I want to dynamically query which objects from a class I would like to retrieve. getattr seems like what I want, and it performs fine for top-level objects in the class. However, I'd like to also specify sub-elements. class MyObj(object): def __init__(self): self.d = {'a':1, 'b':2} self.c = 3 myobj = MyObj() val = getattr(myobj, "c") print val # Correctly prints 3 val = getattr(myobj, "d['a']") # Seemingly incorrectly formatted query print val # Throws an AttributeError How can I get the

Python, using two variables in getattr?

我怕爱的太早我们不能终老 提交于 2019-12-11 17:32:09
问题 I'm trying to do the following: import sys; sys.path.append('/var/www/python/includes') import functionname x = 'testarg' fn = "functionname" func = getattr(fn, fn) func (x) but am getting an error: "TypeError: getattr(): attribute name must be string" I have tried this before calling getattr but it still doesn't work: str(fn) I don't understand why this is happening, any advice is appreciated 回答1: It sounds like you might be wanting locals() instead of getattr() ... x = 'testarg' fn =

How to handle & return both properties AND functions missing in a Python class using the __getattr__ function?

大兔子大兔子 提交于 2019-12-11 02:43:02
问题 It is fairly easy to use the __getattr__ special method on Python classes to handle either missing properties or functions, but seemingly not both at the same time. Consider this example which handles any property requested which is not defined explicitly elsewhere in the class... class Props: def __getattr__(self, attr): return 'some_new_value' >>> p = Props() >>> p.prop # Property get handled 'some_new_value' >>> p.func('an_arg', kw='keyword') # Function call NOT handled Traceback (most

C++ equivalent of Python getattr

自闭症网瘾萝莉.ら 提交于 2019-12-10 16:59:57
问题 In python, there is a function called getattr which would look like this: class MyObject(): def __init__(self): self.xyz = 4 obj = MyObject() getattr(obj, 'xyz') where the call to getattr would return 4. Is there a similar way to do this in C++ (Not Visual C++)? Are there any libraries that have this functionality where I can lookup an object's member variables using a string? I am trying to find a way to look up public data members in a C++ class that I cannot change. So I cannot use a map

Is there a function like __getattr__ that gets called for class variables?

自作多情 提交于 2019-12-10 11:03:02
问题 I want to redefine an object's get function so that I can intercept when any attribute is requested. Using the getattr function, it doesn't catch when existing variables (eg. attr1 in this case) are requested. Is there a way around this, so I can run a function when attr1 is requested? class Test(object): attr1 = 1 def __init__(self): self.attr2 = 1 def __getattr__(self, attr): print 'GETATTR', attr a = Test() a.attr1 a.attr2 a.attr3 Output: GETATTR attr3 I'd like to see GETATTR attr1 and

Can XML-RPC methods be called by name (as strings) in Python?

守給你的承諾、 提交于 2019-12-10 10:32:49
问题 In python, calling XML-RPC methods involves invoking methods on a proxy object: from xmlrpclib import ServerProxy print ServerProxy('https://example.com/rpc').api.hello_there('John') In some other languages, like perl, you pass your method name as a method parameter. use Frontier::Client; $p = Frontier::Client->new(url => 'https://example.com/rpc'); $result = $p->call('api.hello_there', 'John'); print $result; Is there a way to invoke XML-RPC methods by name, as strings, in Python? 回答1: Just

how to make classes with __getattr__ pickable

和自甴很熟 提交于 2019-12-10 09:36:20
问题 How can I modify the classes below to make them pickeable? This question: How to make a class which has __getattr__ properly pickable? is similar but refer to wrong exception in the use of getattr . This other question seems to provide meaningful insight Why does pickle.dumps call __getattr__?, however it fails to provide an example, and I honestly cannot understand what I am suppose to implement. import pickle class Foo(object): def __init__(self, dct): for key in dct: setattr(self, key, dct