getattr

PHP approach to python's magic __getattr__()

℡╲_俬逩灬. 提交于 2019-12-22 08:42:45
问题 I was wondering if there was some way in PHP to duplicate some of the magic of Python attribute/key access. I use a Mongo ORM class written by Steve Lacey called Minimongo in which he utilizes the __getattr__ and __getitem__ to reroute key and attribute flavored access and preserve the 'document-oriented' nature of Mongo. val = doc.foo and val = doc['foo'] become equivalent. I was wondering if there is a similar interface in PHP that would allow the changing of how object access is handled

Why can't I use __getattr__ with Django models?

若如初见. 提交于 2019-12-22 04:08:58
问题 I've seen examples online of people using __getattr__ with Django models, but whenever I try I get errors. (Django 1.2.3) I don't have any problems when I am using __getattr__ on normal objects. For example: class Post(object): def __getattr__(self, name): return 42 Works just fine... >>> from blog.models import Post >>> p = Post() >>> p.random 42 Now when I try it with a Django model: from django.db import models class Post(models.Model): def __getattr__(self, name): return 42 And test it on

doc for __getattr__ defined attributes

孤人 提交于 2019-12-21 05:17:09
问题 I have to customize __getattr__ to call another function to read. This works well except the help(object.attr) does not work. This code is used in an interactive environment, so the help() becomes important to us. Is there a better design to achieve same features but with help() working well. 回答1: The text that is used for "help" is indeed the " __doc__ " attribute of an object. The matter is that depending on the object you have, you can't simply set the __doc__ attribute on it. If what you

Python using getattr to call function with variable parameters

99封情书 提交于 2019-12-21 03:17:07
问题 I'm using getattr to call different functions depending on a variable. Im doing something like that: getattr(foo, bar) () That works, calling functions like foo.bar() My problem is that I have 'bar' functions and I want to call it with different parameters. For example: def f1() : pass def f2(param1) : pass def f3(param1,param2) : pass so 'bar' could be f1, f2, or f3 I tried this: assumming that params is a list which contains all parameters needed for the 'bar' function getattr(foo, bar)

How to get foreign key values with getattr from models

吃可爱长大的小学妹 提交于 2019-12-20 10:43:51
问题 i have a model Project and i am getting the attributes of that with the following instr attr = getattr(project, 'id', None) project is the instance, id is the field and None is the default return type. my question is what if i want to get the F.K keys with this? Get customer name project.customer.name How to get customer name with the above condition? Already Tried if callable(attr): context[node][field] = '%s' % attr() Current Code context = {'project': {}} fields = ('id', 'name', 'category'

How to iterate through a module's functions [duplicate]

旧街凉风 提交于 2019-12-19 05:35:12
问题 This question already has answers here : How to list all functions in a Python module? (14 answers) Closed 5 years ago . I have this function call after importing foo.py. Foo has several methods that I need to call e.g. foo.paint, foo.draw: import foo code if foo: getattr(foo, 'paint')() I need to use a while loop to call and iterate through all the functions foo.paint, foo.draw etc. How do i go about it? 回答1: You can use foo.__dict__ somehow like this: for name, val in foo.__dict__.iteritems

How to iterate through a module's functions [duplicate]

折月煮酒 提交于 2019-12-19 05:34:13
问题 This question already has answers here : How to list all functions in a Python module? (14 answers) Closed 5 years ago . I have this function call after importing foo.py. Foo has several methods that I need to call e.g. foo.paint, foo.draw: import foo code if foo: getattr(foo, 'paint')() I need to use a while loop to call and iterate through all the functions foo.paint, foo.draw etc. How do i go about it? 回答1: You can use foo.__dict__ somehow like this: for name, val in foo.__dict__.iteritems

Python Chain getattr as a string

爱⌒轻易说出口 提交于 2019-12-18 16:47:48
问题 import amara def chain_attribute_call(obj, attlist): """ Allows to execute chain attribute calls """ splitted_attrs = attlist.split(".") current_dom = obj for attr in splitted_attrs: current_dom = getattr(current_dom, attr) return current_dom doc = amara.parse("sample.xml") print chain_attribute_call(doc, "X.Y.Z") In oder to execute chain attribute calls for an object as a string, I had to develop the clumsy snippet above. I am curious if there would be a more clever / efficient solution to

'super' object not calling __getattr__

╄→гoц情女王★ 提交于 2019-12-17 19:12:24
问题 I have one object wrapped inside another. The "Wrapper" accesses the attributes from the "Wrapped" object by overriding __getattr__ . This works well until I need to override an atribute on a sub class, and then access the attribute from the base class using super() . I can still access the attribute directly from __getattr__ but why does super() not work? class Wrapped(object): def __init__(self, value): self.value = value def hello_world(self): print 'hello world', self.value class Wrapper

How do I implement __getattribute__ without an infinite recursion error?

半腔热情 提交于 2019-12-17 04:12:35
问题 I want to override access to one variable in a class, but return all others normally. How do I accomplish this with __getattribute__ ? I tried the following (which should also illustrate what I'm trying to do) but I get a recursion error: class D(object): def __init__(self): self.test=20 self.test2=21 def __getattribute__(self,name): if name=='test': return 0. else: return self.__dict__[name] >>> print D().test 0.0 >>> print D().test2 ... RuntimeError: maximum recursion depth exceeded in cmp