Extending the 'function' built-in class

G

G.

Hi, I can't figure out how I can extend the 'function' built-in class. I tried:
class test(function):
def test(self):
print("test")
but I get an error. Is it possible ?

Regards, G.
 
R

Roy Smith

G. said:
Hi, I can't figure out how I can extend the 'function' built-in class. I
tried:
class test(function):
def test(self):
print("test")
but I get an error. Is it possible ?

Regards, G.

It really helps to give us some basic information when asking questions.
To start, what version of Python are you using, and what error message
do you get?

At least in Python 2 (but, I'm guessing, maybe, you're using Python 3,
since you put parens in your print() statement?), there is no built-in
class called "function". There are built-in functions, and they are of
type builtin_function_or_method. When I try to subclass that by doing:

class foo(type(open)):
pass

I get:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
type 'builtin_function_or_method' is not an acceptable base type

So, we're back to asking what version you're using and what error
message you got.
 
G

G.

Le 01-12-2013 said:
class foo(type(open)):
pass

I get:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
type 'builtin_function_or_method' is not an acceptable base type

So, we're back to asking what version you're using and what error
message you got.

Hi, I don't care that much for the version, since I wanted rather to perform
some tests. I tried your code with various versions and got the same message
than yours. Thus I guess the type can't be extended. Regards, G.
 
G

Gary Herron

Hi, I can't figure out how I can extend the 'function' built-in class. I tried:
class test(function):
def test(self):
print("test")
but I get an error. Is it possible ?

Regards, G.

What error do you get?
What version of Python?
What OS?

And in particular: What 'function' built-in class? I know of no such
thing, and the error message I get with your code says exactly that:
NameError: name 'function' is not defined
Did you not get that same error?

All of which begs the questions: What do you think the function class
is, and why are you trying to extend it?

Gary Herron
 
T

Tim Chase

Hi, I can't figure out how I can extend the 'function' built-in
class. I tried: class test(function):
def test(self):
print("test")
but I get an error. Is it possible ?

While I don't have an answer, I did find this interesting. First,
"function" doesn't seem to be in the default __buitin__ namespace:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'function' is not defined

I presume you're doing it with the following:
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
type 'function' is not an acceptable base type

but, as you mention, the inability to subclass it is somewhat
peculiar. It appears to be metaclass-related.

I'm not quite sure *why* one might want to subclass FunctionType, but
I'm also not sure why you should be *prevented* from subclassing it.

-tkc
 
G

G.

Le 01-12-2013 said:
And in particular: What 'function' built-in class? I know of no such
thing, and the error message I get with your code says exactly that:
NameError: name 'function' is not defined
Did you not get that same error?

Yes, indeed. The 'function' built-in class was the following one:<type 'function'>

but I am interested by answers concerning other similar types also.

Regards, G.
 
M

Mark Janssen

Hi, I can't figure out how I can extend the 'function' built-in class. I tried:
class test(function):
def test(self):
print("test")
but I get an error. Is it possible ?

It has to do with differing models of computation, and python isn't
designed for this. Perhaps you're searching for the ultimate lambda?.
 
A

alex23

Hi, I can't figure out how I can extend the 'function' built-in class. I tried:
class test(function):
def test(self):
print("test")
but I get an error. Is it possible ?

Others have pointed out that you cannot subclass the function type.
Could you explain what you're trying to achieve? It's possible you could
use a decorator instead:

def test(fn):
def _test():
print('test')
fn.test = _test
return fn

@test
def foo():
pass
test

(Note that I've only included _test inside the decorator to show that
you can create a closure to include the wrapped function, as a way of
replicating 'self' in your class definition.)
 
S

Steven D'Aprano

Hi, I can't figure out how I can extend the 'function' built-in class. I
tried:
class test(function):
def test(self):
print("test")
but I get an error. Is it possible ?


You cannot subclass the function type directly, but you can extend
functions.

Firstly, rather than subclassing, you can use delegation and composition.
Google for more info on delegation and composition as an alternative to
subclassing:

https://duckduckgo.com/html/?q=delegation as alternative to%
20subclassing


You can also add attributes to functions:

def spam():
pass

spam.eggs = 23


Want to add a method to a function? You can do that too:

from types import MethodType
spam.method = MethodType(
lambda self, n: "%s got %d as arg" % (self.__name__, n),
spam)


It's even simpler if it doesn't need to be a method:

spam.function = lambda n: "spam got %d as arg" % n)

Want more complex behaviour? Write a callable class:

class MyCallable(object):
def __call__(self, arg):
pass

func = MyCallable()



There are plenty of ways to extend functions. Subclassing isn't one of
them.
 
G

G.

Le 02-12-2013 said:
There are plenty of ways to extend functions. Subclassing isn't one of
them.

Thank you very mych for your complete answer; I falled back to your last
proposal by myself in my attempts; I am happyt to learn about the other ways
also.

Regards, G.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top