473,386 Members | 1,827 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,386 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 1016
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.