Writing a function from within Python

A

Aaron Scott

Is it possible to change the content of a function after the function
has been created? For instance, say I make a class:

class MyClass:
def ClassFunction(self):
return 1

And I create an object:

MyObject = MyClass()

Is there any way to change the content of the function, a la pseudo-
code:

MyObject.ClassFunction = "return 2"

Thanks for any insight you might be able to offer.
 
A

Arnaud Delobelle

Aaron Scott said:
Is it possible to change the content of a function after the function
has been created? For instance, say I make a class:

class MyClass:
def ClassFunction(self):
return 1

And I create an object:

MyObject = MyClass()

Is there any way to change the content of the function, a la pseudo-
code:

MyObject.ClassFunction = "return 2"

Yes there is:
.... def __init__(self, x):
.... self.x = x
.... def bar(self):
.... return self.x
.... .... return self.x + ', yummy!'
.... 'spam, yummy!'


HTH
 
G

George Sakkis

Is it possible to change the content of a function after the function
has been created? For instance, say I make a class:

class MyClass:
def ClassFunction(self):
return 1

And I create an object:

MyObject = MyClass()

Is there any way to change the content of the function, a la pseudo-
code:

MyObject.ClassFunction = "return 2"

Thanks for any insight you might be able to offer.

The short answer is "yes". The longer answer is "yes but most likely
there are better approaches to whatever you want to do". If you give a
bit more context of the general problem you're trying to solve, I'm
sure you'll get more helpful replies.

George
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top