Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #60832 > unrolled thread
| Started by | "G." <grumsk@grumsk.tz> |
|---|---|
| First post | 2013-12-01 19:18 +0000 |
| Last post | 2013-12-02 09:32 +0000 |
| Articles | 11 — 8 participants |
Back to article view | Back to comp.lang.python
Extending the 'function' built-in class "G." <grumsk@grumsk.tz> - 2013-12-01 19:18 +0000
Re: Extending the 'function' built-in class Roy Smith <roy@panix.com> - 2013-12-01 14:30 -0500
Re: Extending the 'function' built-in class "G." <grumsk@grumsk.tz> - 2013-12-01 19:37 +0000
Re: Extending the 'function' built-in class Tim Chase <python.list@tim.thechases.com> - 2013-12-01 13:43 -0600
Re: Extending the 'function' built-in class Gary Herron <gary.herron@islandtraining.com> - 2013-12-01 11:38 -0800
Re: Extending the 'function' built-in class "G." <grumsk@grumsk.tz> - 2013-12-01 20:13 +0000
Re: Extending the 'function' built-in class Robert Kern <robert.kern@gmail.com> - 2013-12-01 20:18 +0000
Re: Extending the 'function' built-in class Mark Janssen <dreamingforward@gmail.com> - 2013-12-01 17:26 -0800
Re: Extending the 'function' built-in class alex23 <wuwei23@gmail.com> - 2013-12-02 12:24 +1000
Re: Extending the 'function' built-in class Steven D'Aprano <steve@pearwood.info> - 2013-12-02 07:01 +0000
Re: Extending the 'function' built-in class "G." <grumsk@grumsk.tz> - 2013-12-02 09:32 +0000
| From | "G." <grumsk@grumsk.tz> |
|---|---|
| Date | 2013-12-01 19:18 +0000 |
| Subject | Extending the 'function' built-in class |
| Message-ID | <529b8ba2$0$2270$426a74cc@news.free.fr> |
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.
[toc] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-12-01 14:30 -0500 |
| Message-ID | <roy-95D382.14302401122013@news.panix.com> |
| In reply to | #60832 |
In article <529b8ba2$0$2270$426a74cc@news.free.fr>,
"G." <grumsk@grumsk.tz> wrote:
> 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.
[toc] | [prev] | [next] | [standalone]
| From | "G." <grumsk@grumsk.tz> |
|---|---|
| Date | 2013-12-01 19:37 +0000 |
| Message-ID | <529b900a$0$3626$426a74cc@news.free.fr> |
| In reply to | #60833 |
Le 01-12-2013, Roy Smith <roy@panix.com> a écrit : > > 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.
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2013-12-01 13:43 -0600 |
| Message-ID | <mailman.3439.1385926961.18130.python-list@python.org> |
| In reply to | #60832 |
On 2013-12-01 19:18, G. wrote:
> 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:
>>> function
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:
>>> from types import FunctionType
>>> class MyFunc(FunctionType):
... 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
[toc] | [prev] | [next] | [standalone]
| From | Gary Herron <gary.herron@islandtraining.com> |
|---|---|
| Date | 2013-12-01 11:38 -0800 |
| Message-ID | <mailman.3440.1385927196.18130.python-list@python.org> |
| In reply to | #60832 |
On 12/01/2013 11:18 AM, G. wrote:
> 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
[toc] | [prev] | [next] | [standalone]
| From | "G." <grumsk@grumsk.tz> |
|---|---|
| Date | 2013-12-01 20:13 +0000 |
| Message-ID | <529b9880$0$2241$426a74cc@news.free.fr> |
| In reply to | #60836 |
Le 01-12-2013, Gary Herron <gary.herron@islandtraining.com> a écrit : > 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(lambda x: 2*x) <type 'function'> but I am interested by answers concerning other similar types also. Regards, G.
[toc] | [prev] | [next] | [standalone]
| From | Robert Kern <robert.kern@gmail.com> |
|---|---|
| Date | 2013-12-01 20:18 +0000 |
| Message-ID | <mailman.3441.1385929133.18130.python-list@python.org> |
| In reply to | #60832 |
On 2013-12-01 19:43, Tim Chase wrote: > 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. Previously: http://grokbase.com/t/python/python-list/033r5nks47/type-function-does-not-subtype#20030324rcnwbkfedhzbaf3vmiuer3z4xq -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
[toc] | [prev] | [next] | [standalone]
| From | Mark Janssen <dreamingforward@gmail.com> |
|---|---|
| Date | 2013-12-01 17:26 -0800 |
| Message-ID | <mailman.3455.1385947612.18130.python-list@python.org> |
| In reply to | #60832 |
> 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?.
--
MarkJ
Tacoma, Washington
[toc] | [prev] | [next] | [standalone]
| From | alex23 <wuwei23@gmail.com> |
|---|---|
| Date | 2013-12-02 12:24 +1000 |
| Message-ID | <l7gr0e$3ge$1@dont-email.me> |
| In reply to | #60832 |
On 2/12/2013 5:18 AM, G. wrote:
> 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
>>> foo.test()
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.)
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2013-12-02 07:01 +0000 |
| Message-ID | <529c3055$0$2869$c3e8da3$76491128@news.astraweb.com> |
| In reply to | #60832 |
On Sun, 01 Dec 2013 19:18:58 +0000, G. wrote:
> 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%20as%20alternative%20to%
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.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | "G." <grumsk@grumsk.tz> |
|---|---|
| Date | 2013-12-02 09:32 +0000 |
| Message-ID | <529c53c0$0$2150$426a74cc@news.free.fr> |
| In reply to | #60856 |
Le 02-12-2013, Steven D'Aprano <steve@pearwood.info> a écrit : > 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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web