473,386 Members | 1,796 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.

Reference to self not passed to member function

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))
....
py> def doit():
.... print "wuzzup?"
....
py> abob = bob(doit=doit)
py>
py> abob.doit()
wuzzup?
Much to my surprise, this works fine.

My questions:

1. What exactly is going on?
2. How can I get ref to self passed to doit() if I want it to? This:

abob.doit(abob)

seems like the hard way

Any ideas?

James
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Jul 19 '05 #1
3 1774
I think it is more clear to rephrase your code as:
-#!/usr/bin/env python
-class bob(object):
- def __init__(self,**kwargs):
- print kwargs
- for fname,func in kwargs.items():
- setattr(self, fname, lambda *args : func(*args))
-
-def doit():
- print "wuzzup?"
-
-
-abob = bob(sayyoudo=doit)
-
-abob.sayyoudo()
outpu is now:
martin@ubuntu:~$ ./test.py
{'sayyoudo': <function doit at 0xb7dfcdf4>}
wuzzup?
martin@ubuntu:~$

so property sayyoudo points to method doit

Jul 19 '05 #2
James Stroud wrote:
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))
...
py> def doit():
... print "wuzzup?"
...
py> abob = bob(doit=doit)
py>
py> abob.doit()
wuzzup? Much to my surprise, this works fine.
1. What exactly is going on?
This behavior shouldn't surprise you. You stored a function as
an attribute. In fact you could have simply done:
py> class bob(object):
.... def __init__(self,**kwargs):
.... for fname, function in kwargs.items():
.... setattr(self, fname, function)
2. How can I get ref to self passed to doit() if I want it to? This:
abob.doit(abob)


py> import new
py> class carol(object):
.... def __init__(self, **kwargs):
.... for name, method in kwargs.items():
.... setattr(self, name,
.... new.instancemethod(method, self, carol))

This should behave as you prefer.
--Scott David Daniels
Sc***********@Acm.Org
Jul 19 '05 #3
Thanks to both Scott and Fredrik.

James

On Friday 06 May 2005 04:22 pm, Scott David Daniels wrote:
James Stroud wrote:
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))
...
py> def doit():
... print "wuzzup?"
...
py> abob = bob(doit=doit)
py>
py> abob.doit()
wuzzup?

Much to my surprise, this works fine.

> 1. What exactly is going on?


This behavior shouldn't surprise you. You stored a function as
an attribute. In fact you could have simply done:
py> class bob(object):
... def __init__(self,**kwargs):
... for fname, function in kwargs.items():
... setattr(self, fname, function)
2. How can I get ref to self passed to doit() if I want it to? This:
abob.doit(abob)


py> import new
py> class carol(object):
... def __init__(self, **kwargs):
... for name, method in kwargs.items():
... setattr(self, name,
... new.instancemethod(method, self, carol))

This should behave as you prefer.
--Scott David Daniels
Sc***********@Acm.Org


--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Jul 19 '05 #4

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

Similar topics

5
by: Jan Pieter Kunst | last post by:
(apologies if this message is a duplicate -- my news server seems to have problems) Greetings, When using PHP 4, this: // ex. 1 class A { function A(&$obj) {
36
by: Riccardo Rossi | last post by:
Hi all! How does Python pass arguments to a function? By value or by reference? Thanks, Riccardo Rossi.
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...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
5
by: Neal Coombes | last post by:
Posted to comp.lang.c++.moderated with little response. Hoping for better from the unmoderated groups: -------- Original Message -------- Subject: Return appropriately by value, (smart)...
2
by: Sandra-24 | last post by:
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) #...
8
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference...
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.