-2

Hello Hope you guys are doing well.

I have a class which has several methods and I have a runner which randomly executes a function out of the several class methods. I have tried using First Class Function methodology in runner function but runner does not recognizes the function names. Can anybody tell my why?

My code:

import random


class A:
    def delete_random_character(self, s):
        """Returns s with a random character deleted"""
        if s == "":
            return s

        pos = random.randint(0, len(s) - 1)
        # print("Deleting", repr(s[pos]), "at", pos)
        return s[:pos] + s[pos + 1:]


    def insert_random_character(self, s):
        """Returns s with a random character inserted"""
        pos = random.randint(0, len(s))
        random_character = chr(random.randrange(32, 127))
        # print("Inserting", repr(random_character), "at", pos)
        return s[:pos] + random_character + s[pos:]


    def flip_random_character(self, s):
        """Returns s with a random bit flipped in a random position"""
        if s == "":
            return s

        pos = random.randint(0, len(s) - 1)
        c = s[pos]
        bit = 1 << random.randint(0, 6)
        new_c = chr(ord(c) ^ bit)
        # print("Flipping", bit, "in", repr(c) + ", giving", repr(new_c))
        return s[:pos] + new_c + s[pos + 1:]


    def runner(self, s):
        """Return s with a random mutation applied"""
        mutators = [
            delete_random_character, ################ Cant recognizes the function why????
            insert_random_character, 
            flip_random_character
        ]
        mutator = random.choice(mutators)
        # print(mutator)
        return mutator(s)
  • 1
    what do you mean by "first class function notation"? That is not standard terminology. – juanpa.arrivillaga Jan 06 '21 at 16:07
  • Anyway, this is because *`delete_random_character`* etc **are not in scope** inside `runner`. This is a basic aspect of python class definitions. Note, your functions don't seem like they should be in a class to begin with, so maybe just move them out into the module scope and then your `runner` method will work. Although, **none** of your methods use any internal state. So really, *why. use a class at all*? – juanpa.arrivillaga Jan 06 '21 at 16:08
  • opppssssie. I didnt try that even though it was on my mind. Tysm @limserhane. – Malik Abdulaziz Jan 06 '21 at 16:08
  • @juanpa.arrivillaga great advice I thought that by this way code will be more organized because I have so many functions in my code. But i should separate the functions into different modules rather than just putting them into classes – Malik Abdulaziz Jan 06 '21 at 16:12
  • Python doesn't require class definitions, like Java. Generally, a class implies some sort of internal state. There is no internal state, indeed, you have a bunch of pure functions (great!), so really, it is more simple and straightforward to have a regular module with a bunch of functions. The *module* is the fundamental unit of code organization in Python – juanpa.arrivillaga Jan 06 '21 at 16:13
  • Thank you. I will surely keep this in mind in future. – Malik Abdulaziz Jan 06 '21 at 16:15
  • Of course, this is veering into an opinion-based territory, but check out this question and an answer I wrote to it that is related to this issue of using classes merely to group functions: https://stackoverflow.com/questions/38758668/grouping-functions-by-using-classes-in-python/ – juanpa.arrivillaga Jan 06 '21 at 16:15
  • You explained it very well @juanpa.arrivillaga – Malik Abdulaziz Jan 06 '21 at 16:23

1 Answers1

0

You are not using references to your class functions initializing mutators; you should

mutators=[
    self.delete_random_character,
    self.insert_random_character, 
    self.flip_random_character
]
limserhane
  • 1,022
  • 1
  • 6
  • 17