Python Forum
Building a method name in a function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Building a method name in a function (/thread-30380.html)



Building a method name in a function - ffgth - Oct-18-2020

Hi,
as an almost beginner, this is just a general knowledge question.
Let's say i have a class foo defining method1, method2, method3.
I'd like to have a function outside class foo, for example:
my_function(fooinstance,name,num),
and build the fooinstance method name by name+num, and then call fooinstance.(name+num).

I'd like to avoid bunch of tests of the kind:
if name==method and num==1: fooinstance.method1()

Is such behavior possible ? Otherwise, gotta code those 40 if..elif i want to avoid.

thanks


RE: Building a method name in a function - Atekka - Oct-18-2020

I'm not sure if you can come up with function names that way but you could put your methods in a list and just loop through the list like

methods=[method1, method2, method3]

for i in range(len(methods)):
    Do your check here 
    
I'm not entirely sure if that's what you're looking for but hope it helps


RE: Building a method name in a function - ffgth - Oct-18-2020

i guess it's basically the same as i would have to access them as foo.methods[0] and foo objects will have no attribute methods.

i still will have to test i in order to call foo.method1, foo.methods[1] will return no attribute error.


RE: Building a method name in a function - Atekka - Oct-19-2020

(Oct-18-2020, 10:55 PM)ffgth Wrote: i guess it's basically the same as i would have to access them as foo.methods[0] and foo objects will have no attribute methods.

i still will have to test i in order to call foo.method1, foo.methods[1] will return no attribute error.

Try
foo.method[1]()

Just use the empty brackets at the end when you need to call the method.


RE: Building a method name in a function - buran - Oct-19-2020

Look at getattr() built-in function.
Note that with attribute name like method1, method2, etc. there is good chance you are doing something the wrong way. Better tell us what/why you are doing this and what's your code, i.e. instead of 40 methods 2 or 3 will be sufficient to demonstrate your case.


RE: Building a method name in a function - deanhystad - Oct-19-2020

If you think of a Python class as a dictionary you have a pretty accurate picture of how it works. When I do a "def method_name" inside the class it adds "method_name" to a dictionary in the class with a value that is the class.method_name function. Creating a class variable does the same thing, except the value in the dictionary is the object referenced by the class variable name.

Since classes are essentially dictionaries with some syntactic sugar, you can add methods from anywhere and at any time. You can also add methods to instances of classes so that two instances do different things when the same method name is used. This is usually a bad idea, but there are times when it can be useful.

I don't know what you are trying to do, but here is something I did which may be similar. I have a class named Model. A model is a collection of models/parts. I have no idea what parts a model may contain or what those parts can do, but I commonly ask all a model's parts to do the same thing. For this I use "forall".
    def forall(self, func_name, *args, **kwargs):
        """Have all my parts execute the named function"""
        for part in self._parts:
            if (func := getattr(part, func_name, None)) is not None:
                func(part, *args, **kwargs)
        return self



RE: Building a method name in a function - ffgth - Oct-19-2020

(Oct-19-2020, 03:27 AM)buran Wrote: Look at getattr() built-in function.
Note that with attribute name like method1, method2, etc. there is good chance you are doing something the wrong way. Better tell us what/why you are doing this and what's your code, i.e. instead of 40 methods 2 or 3 will be sufficient to demonstrate your case.

getattr may do the job, basically I got 40 Custom GraphviewWidget that are in a ui class for diaplaying resources of a n servers (CPU, RAM, load average, I/O, etc..). All these are embedded in a TabWidget, and i have to display Server_Name_Resource_method in each tab.
Design is probably not best but simple :)

Thanks anyway for your answers, will test getattr at once.


RE: Building a method name in a function - buran - Oct-19-2020

I think you need one method and it should take argument (i.e. which widget), not have separate method for each widget (which in turn you will try to call). Note, the widgets should also be in some sort of container, not stored in separate variables/names (you are unclear as to that part)


RE: Building a method name in a function - ffgth - Oct-19-2020

indeed, i'm accessing the attributes (and not the methods) generated through Qt designer. gettattr did the job and saved me 40 conditions :)


RE: Building a method name in a function - buran - Oct-19-2020

(Oct-19-2020, 01:01 PM)ffgth Wrote: i'm accessing the attributes (and not the methods)

attributes
sometimes are grouped into/referred to as properties and methods. So all methods are attributes but not all attributes are methods.