Oh well. I had wanted to be able to define two functions f and g, and
have f*g be the composition of f and g. func_type = type(lambda: None) class composable_function(func_type):
... def __mult__(f,g):
... def c(*args, **kw):
... return f(g(*args, **kw))
... return c
...
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: Error when calling the metaclass bases
type 'function' is not an acceptable base type
Seems like a wart to me. 5 2634
Paul Rubin wrote: Oh well. I had wanted to be able to define two functions f and g, and have f*g be the composition of f and g.
>>> func_type = type(lambda: None) >>> class composable_function(func_type): ... def __mult__(f,g): ... def c(*args, **kw): ... return f(g(*args, **kw)) ... return c ... Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: Error when calling the metaclass bases type 'function' is not an acceptable base type >>>
Seems like a wart to me.
Well - function inheritance is not known so far in python - and in no
other language I know.
How do you expect to create f and g, even if above construct would work?
Basdically you want __mult__ being part of f or g when python encounters
something like this
f * g
But then how did you plan to declare f?
def f(composable_function)(x):
pass
obviously won't work.
So the only way to achieve this with current semantics is to make f anf
g objects with a call methods. In that very moment, you're done - as
extending from object is no problem :)
class ComposeableFunction(object):
def __call__(self, *args, **kwargs):
return self.second(self.first(*args, **kwargs))
def __mul__(self, other):
nc = ComposeableFunction()
nc.first = other
nc.second = self
return nc
class f(ComposeableFunction):
def __call__(self, x):
return x * 2
class g(ComposeableFunction):
def __call__(self, x):
return x + 2
f = f()
g = g()
print f(4)
print g(4)
print (f*g)(4)
Diez
Diez B. Roggisch wrote: Paul Rubin wrote:
Oh well. I had wanted to be able to define two functions f and g, and have f*g be the composition of f and g.
>>> func_type = type(lambda: None) >>> class composable_function(func_type): ... def __mult__(f,g): ... def c(*args, **kw): ... return f(g(*args, **kw)) ... return c ... Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: Error when calling the metaclass bases type 'function' is not an acceptable base type >>>
Seems like a wart to me.
So the only way to achieve this with current semantics is to make f anf g objects with a call methods. In that very moment, you're done - as extending from object is no problem :)
class ComposeableFunction(object):
def __call__(self, *args, **kwargs): return self.second(self.first(*args, **kwargs))
Note, with a little bit of massaging, you can turn ComposableFunction
into a decorator, for more natural function definition:
(Untested, I'm not on a system with Py2.4 at the moment):
class Composable(object):
def __init__(self,f):
self.callable = f
def __call__(self,*args, **kwargs):
return self.callable(*args, **kwargs)
def __mul__(self,other):
return Composable(lambda (*a, **kwa): self.callable(other(*a,
**kwa)))
Usage:
@Composable
def f(x):
return x**2
@Composable
def g(x):
return x+1
# And we shouldn't even need a @Composable on the last in the sequence
def h(x):
return x/2.0 f(1)
1(f*g)(1)
4(f*g*h)(2)
4
This might not combine neatly with methods, however; the bound/unbound
method magic is still mostly voodoo to me.
"Diez B. Roggisch" <de***@nospam.web.de> writes: Well - function inheritance is not known so far in python - and in no other language I know.
Yeah, I didn't really expect it to work, but it seems like a logical
consequence of type/class unification.
Basically you want __mult__ being part of f or g when python encounters something like this
f * g
But then how did you plan to declare f?
Come to think of it, that's also a wart. I'd been thinking of
using a decorator, as Christopher Subich suggested,
@composable
def f(x): ...
but it's not how the decorator could actually work (other than through
gross CPython-specific hacks).
So the only way to achieve this with current semantics is to make f anf g objects with a call methods. In that very moment, you're done - as extending from object is no problem :)
Yeah, I thought of that, but felt it wasn't in the proper spirit :)
If you google a bit on the newsgroup, you should find a message
from me asking about the ability to subclass FunctionType, and
a reply from Tim Peters saying that the only reason why this
was not done is lack of developer time and the fact that this was
not considered an important priority.
Michele Simionato
"Michele Simionato" <mi***************@gmail.com> writes: If you google a bit on the newsgroup, you should find a message from me asking about the ability to subclass FunctionType, and a reply from Tim Peters saying that the only reason why this was not done is lack of developer time and the fact that this was not considered an important priority.
Yeah, I just thought of it as a perverse but sort of cute way to
implement composition and similar operations on functions. It could
be handy though, and simple to implement, if functions supported the
'*' operator for composition. The example with a callable class is
a possible workaround, but ugly. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: |
last post by:
Could someone help extend this ChangeImage function to also display a
different hyperlink underneath the newly selected image?:
<script language="javascript">
<!--
/*Combo Box Image Selector:...
|
by: Mark G |
last post by:
I want to extend the DataGridTextBox so I can access its IsInputKey protected method. I created the below class to do this
Public Class myDataGridTextBo
Inherits DataGridTextBo
Public...
|
by: Alan Bashy |
last post by:
Please, guys, In need help with this. It is due in the next week. Please,
help me to implement the functions in this programm especially the first
three constructor. I need them guys. Please, help...
|
by: Jerzy Karczmarczuk |
last post by:
Gurus, before I am tempted to signal this as a bug, perhaps
you might convince me that it should be so. If I type
l=range(4)
l.extend()
l gives , what else...
On the other hand, try
|
by: Boobie |
last post by:
I switched to using this function to create element:
----------------------------------------------------
function elem(name, attrs, style, text) {
var e = document.createElement(name);
if...
|
by: etuncer |
last post by:
Hello All,
I have Access 2003, and am trying to build a database for my small
company. I want to be able to create a word document based on the data
entered through a form. the real question is...
|
by: rick |
last post by:
hi
Is there a DB2 equivalent of Informix.Extend function?
or please help me the best way to create one
Thanks
rick
|
by: jacobstr |
last post by:
I've noticed Object.extend used in a few different ways and I'm having
trouble distinguishing why certain usages apply to a given situation.
On line 804 Ajax.Base is defined as follows:
...
|
by: daniel2335 |
last post by:
This problem is everywhere! I have read all forums available, spent about 40+ hours on it and lost a bit of hair. I was going to explain what its ment to look like but I just realised removing the...
|
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: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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:
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...
|
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: 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...
|
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: 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,...
| |