0

I have a Data strukture with differet levels like this: Data1--Methode1--Data2--Methode2--Data3 Data2=Data1.methode1

Each level of data need different method to approach. I am wondering if i can build a list of Methode to "dig in", for example [Method1,Method2,...]. And I will write one loop to "dig" the data by trying all the methods in the list.

I have this idea because we type string in screen when we call a method in python, for example I type print() to call print function. Can we make the string of a method replaceable, so i can change print() to list() or something else?

So can i build such a "Method list"? If I can, what data type sould I use?

2
  • 1
    This question should include more details and clarify the problem. Commented Jan 4, 2022 at 18:14
  • @Tibebes.M what lane do is almost what i want. But In my case Data is called with for example: Data2=Data1.methode1 Commented Jan 4, 2022 at 18:43

1 Answer 1

1

In python methods can be treated like variables.

Below is an example of two different method functions that contain no input arguments.

def method1():
    # do something
    return 1

def method2():
    # do something else
    return 2


methods = [method1, method2]

for m in methods:
    print(m())

You need to be careful about arguments, it makes it easier if they all expect the same input. Below is another example where two methods are added to a list, they both expect a single integer as an argument.

def method1(arg):
    # do something
    return arg*1

def method2(arg):
    # do something else
    return arg*2


methods = [method1, method2]

for m in methods:
    print(m(5))

Furthermore, we can consider the case where a method is trying something that may not work. For this we can add a try / except block to the loop. This will continue the loop if the method we are trying throws an exception.

def method0(arg):
    return arg/(arg - 5)

def method1(arg):
    # do something
    return arg*1

def method2(arg):
    # do something else
    return arg*2

methods = [method0, method1, method2]

for m in methods:
    try:
        print(m(5))
    except:
        print(f'{m.__name__} did not work')

produces the output

method0 did not work
5
10

Lets say you have a class object with functions/methods inside. Those can be added to a list like this:

class DataType:
    val = 5
    def method0(self, arg):
        return arg/(arg - self.val)
    
    def method1(self, arg):
        # do something
        return arg*1
    
    def method2(self, arg):
        # do something else
        return arg*2

# instantiate the class object
obj = DataType()

# add methods to list from the object
methods = [obj.method0, obj.method1, obj.method2]

for m in methods:
    try:
        print(m(5))
    except:
        print(f'{m.__name__} did not work')

Producing the same output as above

method0 did not work
5
10
Sign up to request clarification or add additional context in comments.

9 Comments

thx, but what if I want to use Data.changeable_methode? Is it also ok with attribute?
I'm not familiar with Data.changeable_methode, is this a method that exists in a class variable?
yeah, I think so. It's a methode from Datatype class.
I added a section that ha a class called DataType, please let me know if this works for you. Your naming conventions may be slightly different but the same principles should apply.
Ur answer is awsome. But in my case I don't know what class it is. I only know the name of method, and can use it. But I can't see where it comes. All the Methode is wrappered in the Module. It is too difficult to understand what is going on there. Thx for ur answer! It inspires me a lot. I know what to do next. I will try to contact the developer of this module.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.