Question

Long story short, I need to find a shortcut to calling the same method in multiple objects which were made from multiple classes.

Now, all of the classes have the same parent class, and even though the method differs bewteen the different classes, I figured the methods be the same name would work. So I though I might just be able to do something like this:

for object in listOfObjects:
    object.method()

It hasn't worked. It might very well be a misspelling by me, but I can't find it. I think I could solve it by making a list that only adds the objects I need, but that would require a lot of coding, including changing other classes. ~~ skip to last paragraph for pseudo code accurately describing what I need~~


At this point, I will begin to go more in detail as to what specifically I am doing. I hope that it will better illustrate the scope of my question, and that the answering of my question will be more broadly applicable. The more general usage of this question are above, but this might help answer the question. Please be aware that I will change the question once I get an answer to more closely represent what I need done, so that it can apply to a wide variety of problems.

I am working on a gravity simulator. Whereas most simulators make objects which interact with one another and represent full bodies where their center of gravity is the actual attraction point, I am attempting to write a program which will simulate the distribution of gravity across all given points within an object.

As such, each object(not in programming terms, in literal terms) is made up of a bunch of tiny objects (both literally and figuratively). Essentially, what I am trying to do is call the object.gravity() method, which essentially takes into account all of the gravity from all other objects in the simulation and then moves the position of this particular object based on that input.

Now, either due to a syntactical bug (which I kinda doubt) or due to Python's limitations, I am unable to get all of the particles to behave properly all at once. The code snippet I posted before doesn't seem to be working.

tl;dr:

As such, I am wondering if there is a way (save adding all objects to a list and then iterating through it) to simply call the .gravity() method on every object that has the method. basically, even though this is sort of list format, this is what I want to do:

for ALL_OBJECTS:
    if OBJECT has .gravity():
        OBJECT.gravity()
Était-ce utile?

La solution

You want the hasattr() function here:

for obj in all_objects:
    if hasattr(obj, 'gravity'):
        obj.gravity()

or, if the gravity method is defined by a specific parent class, you can test for that too:

for obj in all_objects:
    if isinstance(obj, Planet):
        obj.gravity()

Autres conseils

Can also do ... better pythonic way to do it

for obj in all_objects:
    try:
        obj.gravity()
    except AttributeError:
        pass

Using getattr while set default option of getattr to lambda: None.

for obj in all_objects:
    getattr(obj, 'gravity', lambda: None)()
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top