Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python how to change functionality of a method during runtime

Tags:

python

Wondering if its is possible to change functionalify of a method during runtime e.g

x = obj1 + obj2
return x+y

and you want to add

x = obj1 + obj2
x+= obj3
return x+y
like image 564
user537638 Avatar asked Dec 10 '10 10:12

user537638


People also ask

How do you stop a method execution in Python?

Python exit commands: quit(), exit(), sys. exit() and os. _exit()

What is Methodtype in Python?

Python type() Method The type() method either returns the type of the specified object or returns a new type object of the specified dynamic class, based on the specified class name, base classes and class body.

How do I call a method in Python?

Once a function is created in Python, we can call it by writing function_name() itself or another function/ nested function. Following is the syntax for calling a function. Syntax: def function_name():

How does a method work in Python?

A method in python is somewhat similar to a function, except it is associated with object/classes. Methods in python are very similar to functions except for two major differences. The method is implicitly used for an object for which it is called. The method is accessible to data that is contained within the class.


1 Answers

In python classes are just objects that can be changed at runtime. So for example:

class Dog:
    def talk(self):
        print "bark"
dog1 = Dog()
dog1.talk()  # --> Bark

def cat_talk(self):
    print "Meow"

Dog.talk = cat_talk
dog1.talk()  # --> Meow

but you don't want to do things like this or whoever will have to maintain or debug this program for sure will try to kill you (and this guy will be probably yourself)

like image 194
6502 Avatar answered Nov 15 '22 06:11

6502