473,287 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Writing a function from within Python

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.
Jun 27 '08 #1
2 1013
Aaron Scott <aa***************@gmail.comwrites:
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:
>>class Foo(object):
.... def __init__(self, x):
.... self.x = x
.... def bar(self):
.... return self.x
....
>>foo = Foo('spam')
foo.bar()
'spam'
>>from types import MethodType
def newbar(self):
.... return self.x + ', yummy!'
....
>>foo.bar = MethodType(newbar, foo)
foo.bar()
'spam, yummy!'
HTH

--
Arnaud
Jun 27 '08 #2
On May 28, 4:39 pm, Aaron Scott <aaron.hildebra...@gmail.comwrote:
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
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dariusz | last post by:
I would like to try writing my own functions, so as to keep some code within the same PHP file rather than make multiple PHP files. What I am having a problem is is how to call a function when a...
6
by: Fernando Rodriguez | last post by:
Hi, What's the syntax to add an attribute to a function form the function body? For example, instead of doing: def fn(): return 1 fn.error = "Error message" print fn.error
17
by: Bart Nessux | last post by:
I understand that most people write functions to reuse code, no? So, I assume it would be better to write functions that are very specific, as opposed to those that are more generic. However, I...
6
by: Marian Aldenhövel | last post by:
Hi, I am using the FMOD audio-library with the pyFMOD python bindings. pyFMOD uses ctypes. It is possible to register callback functions with FMOD that are called at certain points in the...
7
by: Pankaj | last post by:
The module which i am creating is like Part A: 1. It does some processing by using python code. 2. The result of this python code execution is written to a text file. ] Part B: 1. I read a...
14
by: James Thiele | last post by:
I'd like to access the name of a function from inside the function. My first idea didn't work. >>> def foo(): .... print func_name .... >>> foo() Traceback (most recent call last): File...
2
by: neeebs | last post by:
Hi, I'm not sure if this is a javascript problem per se, but here goes. I have an xsl document with a python function defined within a <script> block. Elsewhere in the xsl file, within a python...
11
by: r0g | last post by:
I'm collecting together a bunch of fairly random useful functions I have written over the years into a module. Generally speaking is it best to a) Import all the other modules these functions...
11
by: Krzysztof Retel | last post by:
Hi guys, I am struggling writing fast UDP server. It has to handle around 10000 UDP packets per second. I started building that with non blocking socket and threads. Unfortunately my approach...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.