473,385 Members | 1,449 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,385 software developers and data experts.

<build-in function> incompatible with <function>

Built-in functions don't bind to classes like regular functions. Is
this intended? (I do notice that the Python Reference Manual sec 3.2
under "Class Instance" refers to a "user-defined function"). Any ideas
what the reason is for this distinction between build-in functions and
normal functions?

It's rather inconvenient when implementing some methods (not the whole
class) in a C extension :-(

$ python
Python 2.4.2 (#1, Nov 3 2005, 12:41:57)
[GCC 3.4.3-20050110 (Gentoo Linux 3.4.3.20050110, ssp-3.4.3.20050110-0,
pie-8.7 on linux2
Type "help", "copyright", "credits" or "license" for more information.
def normal_func(x): .... return x
.... class foo(object): .... a = normal_func
.... b = lambda x : x
.... c = abs
.... obj = foo()
obj.a <bound method foo.normal_func of <__main__.foo object at 0xb7c3766c>> obj.b <bound method foo.<lambda> of <__main__.foo object at 0xb7c3766c>> obj.c

<built-in function abs>

Jan 31 '06 #1
5 1571

"Luke" <lu**@deller.id.au> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Built-in functions don't bind to classes like regular functions. Is
this intended? (I do notice that the Python Reference Manual sec 3.2
under "Class Instance" refers to a "user-defined function"). Any ideas
what the reason is for this distinction between build-in functions and
normal functions?


no, but does this help...
class Test: .... c = classmethod(abs)
.... obj = Test()
obj.c <bound method classobj.abs of <class __main__.Test at 0x00B2C060>>

Emile

Jan 31 '06 #2
Luke wrote:
Built-in functions don't bind to classes like regular functions. Is
this intended? (I do notice that the Python Reference Manual sec 3.2
under "Class Instance" refers to a "user-defined function"). Any ideas
what the reason is for this distinction between build-in functions and
normal functions?

It's rather inconvenient when implementing some methods (not the whole
class) in a C extension :-(

$ python
Python 2.4.2 (#1, Nov 3 2005, 12:41:57)
[GCC 3.4.3-20050110 (Gentoo Linux 3.4.3.20050110, ssp-3.4.3.20050110-0,
pie-8.7 on linux2
Type "help", "copyright", "credits" or "license" for more information.
def normal_func(x):
... return x
...
class foo(object):
... a = normal_func
... b = lambda x : x
... c = abs
...
obj = foo()
obj.a
<bound method foo.normal_func of <__main__.foo object at 0xb7c3766c>>
obj.b
<bound method foo.<lambda> of <__main__.foo object at 0xb7c3766c>>
obj.c


<built-in function abs>


py> import types
py> def doit(x):
.... print x
....
py> class bob:
.... pass
....
py> b = bob
py> b.x = types.MethodType(doit, b)
py> b.x
<bound method ?.doit of <class __main__.bob at 0x403d8b3c>>
py> b.x()
__main__.bob
Jan 31 '06 #3
Thanks James, though from the output of b.x() it appears that x is a
class method (ie the class is passed as the first parameter rather than
the instance)...

Jan 31 '06 #4
Luke wrote:
Thanks James, though from the output of b.x() it appears that x is a
class method (ie the class is passed as the first parameter rather than
the instance)...


Sorry, the one line was probably supposed to be

b = bob()

I forgot the parens:

py> b = bob()
py> b.x = types.MethodType(doit, b)
py> b.x()
<__main__.bob instance at 0x404afb6c>
py> b.x
<bound method ?.doit of <__main__.bob instance at 0x404afb6c>>

James
Jan 31 '06 #5
James Stroud wrote:
Luke wrote:
Thanks James, though from the output of b.x() it appears that x is a
class method (ie the class is passed as the first parameter rather than
the instance)...


Sorry, the one line was probably supposed to be

b = bob()

I forgot the parens:

py> b = bob()
py> b.x = types.MethodType(doit, b)
py> b.x()
<__main__.bob instance at 0x404afb6c>
py> b.x
<bound method ?.doit of <__main__.bob instance at 0x404afb6c>>

James


Also, you should know about the __abs__ method (this is probably what
you are really looking for:

py> class bob:
.... def __init__(self, aval):
.... self.value = aval
.... def __abs__(self):
.... return abs(self.value)
....
py> b = bob(-4)
py>
py> abs(b)
4
py> b.value
-4
You may want to have a look at this:

http://docs.python.org/ref/customization.html
Jan 31 '06 #6

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

Similar topics

3
by: Jaroslaw Zabiello | last post by:
I got strange errors in Zope 2.7. METALError macro 'context/base' has incompatible version None, at line 1, column 1 One ZPT file (named 'base') defines some simply slots: <html...
0
by: Neil Sargent | last post by:
This is a general posting of how I fixed a problem created by using the Access 97 ODE Setup Wizard on a Windows XP machine. I hope it helps anyone who comes across the problem saves them the 3 days...
7
by: Brian Stubblefield | last post by:
Dear clc members, I am new to C and am posting several messages concerning a large C program that I am debugging. I am encountering a "incompatible types in assignment" warning for the...
1
by: Josh Wilson | last post by:
Hey gang, So I have my stdin which is user defined file, and am wanting to write it to a temporary file (seems odd I know, but there is a sincere reason), and then use that file as the stdin for...
7
by: Arun Kannan | last post by:
Hi We are planning to upgrade a MFC Application (.Exe) to compile on Dot Net Framework. I have opened the application in Visual Studio 2003. I changed the compiler option to /clr. I have set...
0
by: Brian Takita | last post by:
Hello, I'm getting the following error at the end of this message when trying to run the ReportManager and the ReportServer: Assembly system.data.dll security permission grant set is...
9
by: Ali | last post by:
incompatible warning in C/C++ I am using MS Visual C/C++ 6.0. I get the following warning when compiling C:model.ex.c(1221) : warning C4133: '=' : incompatible types - from 'struct s_lsp_def...
6
by: PraZ | last post by:
Hi all. Here is a simple code, which when compiled with gcc results in the warning "incompatible pointer type" for arg 1, as expected. But this is just what I want to do, because it makes it...
8
by: fei.liu | last post by:
I have the following source code. It seems wierd to me why gca's value cannot be reassigned. It's afterall a pointer and has a pointer value. I am aware that the standard says it's not allowed. But...
0
by: srikar | last post by:
Hi all, I am having a problem, when I am compiling the code in 32 bit option on a 64 bit machine using the macro CPPFLAGS= -m32 I am getting the following warnings from the linker . ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.