473,387 Members | 1,578 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,387 software developers and data experts.

Prevent self being passed to a function stored as a member variable?

How can you prevent self from being passed to a function stored as a
member variable?

class Foo(object):
def __init__(self, callback):
self.func = callback

f =Foo(lambda x: x)
f.func(1) # TypeError, func expects 1 argument, recieved 2

I thought maybe you could do this:

class Foo(object):
def __init__(self, callback):
self.func = staticmethod(callback) # Error, staticmethod not
callable

Somebody (maybe everybody other than myself) here must know?

Thanks,
-Sandra

Sep 4 '06 #1
2 1470
On 4 Sep 2006 09:39:32 -0700, Sandra-24 <sa***********@yahoo.comwrote:
How can you prevent self from being passed to a function stored as a
member variable?

class Foo(object):
def __init__(self, callback):
self.func = callback

f =Foo(lambda x: x)
f.func(1) # TypeError, func expects 1 argument, recieved 2
Do you really get that error? I got the following in my python shell:

..>>class Foo(object):
..... def __init__(self, callback):
..... self.func = callback
.....
..>>f = Foo(lambda x: x)
..>>f.func(1)
1
Sep 4 '06 #2
Qiangning Hong wrote:
Do you really get that error?
Sorry, my bad. You're correct of course. I had accidentally passed an
object, by naming it the same as the function, instead of my function,
and the object had __call__ defined, and took exactly two parameters,
just like my function, but one of them beign self. So when I passed two
arguments, it got three, and that's how I was confused into thinking
self was being passed to my function (because it was, but not to my
function.) All a big foulup on my part. I might have worked on that
problem for a while before figuring it out, thanks!

-Sandra

Sep 4 '06 #3

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

Similar topics

2
by: Hayden Kirk | last post by:
Hey Is it possible to do without $this? I have got an instance of another class inside a class. Now I want to access this classes functions using the self keyword, but it seems i cant. Or...
3
by: James Stroud | last post by:
Hello All, I did this: py> class bob(object): .... def __init__(self,**kwargs): .... for fname,func in kwargs.items(): .... setattr(self, fname, lambda *args : func(*args)) .......
5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
3
by: jimjim | last post by:
Hello, My question concerns as to how a pointer is passed by reference as a function argument. The following is from code taken from the MICO implementation of the CORBA specification. in...
20
by: Wayne Sutton | last post by:
OK, I'm a newbie... I'm trying to learn Python & have had fun with it so far. But I'm having trouble following the many code examples with the object "self." Can someone explain this usage in...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
24
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When...
8
by: ssecorp | last post by:
I first learned about OO from Java. I much prefer to program in Python though. However I am consufed about 2 things. 1. Why do I have to pass self into every method in a class? Since I am...
4
by: Immortal_Nephi | last post by:
I had a lot of research to see how function pointer works. Sometimes, programmers choose switch keyword and function in each case block can be called. Sometimes, they choose ordinary function...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.