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/ 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
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
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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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) {
|
by: Riccardo Rossi |
last post by:
Hi all!
How does Python pass arguments to a function? By value or by reference?
Thanks,
Riccardo Rossi.
|
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...
|
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...
|
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)...
|
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) #...
|
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...
|
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...
|
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'
|
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,...
|
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$) {
}
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
| |