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> 5 1590
"Luke" <lu**@deller.id .au> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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
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.MethodTyp e(doit, b)
py> b.x
<bound method ?.doit of <class __main__.bob at 0x403d8b3c>>
py> b.x()
__main__.bob
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)...
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.MethodTyp e(doit, b)
py> b.x()
<__main__.bob instance at 0x404afb6c>
py> b.x
<bound method ?.doit of <__main__.bob instance at 0x404afb6c>>
James
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.MethodTyp e(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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
xmlns="http://www.w3.org/1999/xhtml"
xml:lang="en-US"
|
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 its taken me to sort out.
The application generates the following error:
"Incompatible version of the RPC stub"
This occurs when trying to run Outlook to send an email. However I expect it
would occur for any operation which uses...
|
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
following code during the compile stage:
#include <stdio.h>
#include <signal.h>
|
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 the remaining part of
the program. I have combed the FAQ and got some preliminary questions
answered, and tried to model this after what I have seen suggested to
people previously, but for my strcpy and freopen I get,
"warning: passing arg 1 of...
|
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 following properties
-Basic Run Time checks to Default
-Smaller Type Check to N
-Runtime Library -Mutlit Threaded Debug threade
-Use of MFC - use Standard windows libraries (tried with MFC as Static and Shared dll.. No luck
Now, i am wondering...
| |
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 incompatible
between appdomains.
I had to apply KB 887787 (http://support.microsoft.com/?kbid=887787) to
fix my previous error, which stated...
Assembly microsoft.web.validatepathmodule.dll security permission grant
|
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 *' to 'struct s_lsp_def *'
In the .h file, I have
======================
|
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 easy for me to handle the
single dimensional stream I have as a multidimensional array inside the
function func(). Now, I am just wondering if there is a way by which I
can disable this incompatible pointer type warning in gcc. Any
suggestions?
|
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 I cannot
convince myself why this is prohibited.
static int ia = {64, 64, 64, 64};
static char ca = {'a', 'b', 'c', 'd'};
static int iaa = {64, 64, 64, 64};
static char caa = {'a', 'b', 'c', 'd'};
static char cab = "abcd";
|
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 .
/`../bin/amd64_stat_gnu_02015/WriteFault.o' is incompatible with i386:x86-64 output
/usr/bin/ld: warning: i386 architecture of input file `../bin/amd64_stat_gnu_02015/WriteLogic.o' is incompatible with i386:x86-64 output
/usr/bin/ld: warning: i386...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |