473,569 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What exactly are bound methods?

Although I see lots of references to them in various documentation, I
can't find a decent explanation of exactly what they are. I'm guessing
that it's a reference to a method that remembers which object it came
from, and that when it's called, it passes that object as the first
parameter (which would conventionally be 'self'). Is this correct?

--
Ben Sizer
Jul 18 '05 #1
3 21126
Kylotan wrote:

Although I see lots of references to them in various documentation, I
can't find a decent explanation of exactly what they are. I'm guessing
that it's a reference to a method that remembers which object it came
from, and that when it's called, it passes that object as the first
parameter (which would conventionally be 'self'). Is this correct?


Yep:
class C: .... def f(self, x):
.... print x
.... c = C() # c is an instance of C
C <class __main__.C at 0x402e141c> C.f # the unbound method <unbound method C.f> c.f # the bound method <bound method C.f of <__main__.C instance at 0x402e4c8c>> C.f(c, 1) # invoking unbound methods requires passing the instance 1 c.f(1) # bound methods already contain the instance

1
--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \
\__/ There are defeats more triumphant than victories.
-- Montaigne
Jul 18 '05 #2

Ben> Although I see lots of references to them in various documentation,
Ben> I can't find a decent explanation of exactly what they are. I'm
Ben> guessing that it's a reference to a method that remembers which
Ben> object it came from, and that when it's called, it passes that
Ben> object as the first parameter (which would conventionally be
Ben> 'self'). Is this correct?

When you define a class like so:

class foo:
def bar(self):
pass

foo.bar is an unbound method. Binding the method associates it with a
particular instance. You can think of a bound method as currying the
instance object with the unbound method. For info on currying, check the
Python Cookbook:

http://aspn.activestate.com/ASPN/Coo...n/Recipe/52549

Skip

Jul 18 '05 #3
ky*****@hotmail .com (Kylotan) wrote in message news:<15******* *************** **@posting.goog le.com>...
Although I see lots of references to them in various documentation, I
can't find a decent explanation of exactly what they are. I'm guessing
that it's a reference to a method that remembers which object it came
from, and that when it's called, it passes that object as the first
parameter (which would conventionally be 'self'). Is this correct?


If you really want to learn the difference between functions, methods
and bound methods, you must learn descriptors:

http://users.rcn.com/python/download/Descriptor.htm

Warning: the study of descriptors may cause your head to explode ...
def f(self): pass .... class C(object): pass .... c=C()
f.__get__(c,C) <bound method C.f of <__main__.C object at 0x017716D0>> f.__get__(None, C) <unbound method C.f>

Jul 18 '05 #4

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

Similar topics

5
2352
by: Marian | last post by:
Hi, I am totaly novice in .NET and I am studying a book about this. There was mentioned "assembly". I did not understand, how function does it has . I would like to know the exact run of code (intermediate language and so on). Is there any page on internet, which makes me clear? Thanx
12
2685
by: Russell E. Owen | last post by:
I have several situations in my code where I want a unique identifier for a method of some object (I think this is called a bound method). I want this id to be both unique to that method and also stable (so I can regenerate it later if necessary). I thought the id function was the obvious choice, but it doesn't seem to work. The id of two...
8
2932
by: Kevin Little | last post by:
#!/usr/bin/env python ''' I want to dynamically add or replace bound methods in a class. I want the modifications to be immediately effective across all instances, whether created before or after the class was modified. I need this to work for both old ('classic') and new style classes, at both 2.3 and 2.4. I of course want to avoid...
16
2650
by: thenightfly | last post by:
Ok, I know all about how binary numbers translate into text characters. My question is what exactly IS a text character? Is it a bitmap?
18
1960
by: cj | last post by:
members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe. I'm under the impression before you can use a class you have to make an instance of it. So how can a class be threadsafe by itself but an instance of it not be? I guess I don't get what exactly being threadsafe means. ...
15
2656
by: arnuld | last post by:
-------- PROGRAMME ----------- /* Stroustrup, 5.6 Structures STATEMENT: this programmes *tries* to do do this in 3 parts: 1.) it creates a "struct", named "jd", of type "address". 2. it then adds values to "jd" 3.) in the end it prints values of "jd".
11
1457
by: Mathias Panzenboeck | last post by:
Hi. I have a problem with weak refs and bound methods. The best explanation for the problem is a short bit of code. I have the three classes Wrapper, Foo and Bar: import weakref class Wrapper(object): def __init__(self,x): self.x = weakref.ref(x)
1
274
by: srinivasan srinivas | last post by:
HI Peter, It works will for instance and class methods. But it doesn't work for static methods. Can you tel me how to pickle static methods as well?? Thanks, Srini ----- Original Message ---- From: Peter Otten <__peter__@web.de> To: python-list@python.org Sent: Wednesday, 2 July, 2008 12:53:19 PM
1
1600
by: webtourist | last post by:
Warning: New learner here Not sure what the term means "implementation of Python". So what exactly does it mean *"implementation of Python"* like cpython, Jython, IronPython ????? In terms of IronPython --- does it mean a Python programmer can write .Net app in Python ? Thanks
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7926
Oralloy
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. ...
1
7679
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...
0
6287
agi2029
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...
1
5514
isladogs
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...
0
5223
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3657
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...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2117
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 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.