471,351 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,351 software developers and data experts.

Unbound methods and functions

Ben
Hi,

I was recently writing a python interpreter (in python), partly to teach
myself more about the python object system, and partly to learn more about
how programming languages work in general.

I started with the getattribute/descriptor system as it seems that this is
in many ways the "core" of the interpreter, and have come across a few
questions.

Firstly, why does the default getattribute first check for data descriptors
in the type object? Is it only for symmetry with setattr, as it appears to
me that if you can't set that name in the instance dictionary then the fall
back to type lookup at the end should always "do the right thing". Of
course, you could override this by explicitly inserting into the instance
dictionary (a['foo'] = ...), but in this case it seems that the least
surprising thing to do would be to return the dictionary version anyway.

I will post my code so far at the end of this message for people to comment
on. The other changes that I have made from the python c code is that:

Object itself contains a lot of the machinery for being a type, and Type
itself does not do very much.

dset (the equiv of __set__) returns whether it actually did anything. This
is becuase I want to reimplement this in c++, and the neatest way I could
come up with for data descriptors was for the default __set__ to do nothing
and return false, which means "fall back to inserting into the instance
dictionary)

Thanks for the feedback in advance

Ben
---

Code follows:

class Object:
def __init__(self, typeval = None):
self.dict = {}
self.typeval = typeval

def getattribute(self, x):
t = self.lookup(x)
if t:
return t.dget(None, self)
t = self.typeval.lookup(x)
if t:
return t.dget(self, self.typeval)
raise AttributeError(x)

def setattribute(self, x, val):
if self.typeval:
t = self.typeval.lookup(x)
if t:
if t.dset(self, val):
return
self.dict[x] = val

def lookup(self, x):
for obj in self.mro():
t = obj.dict.get(x)
if t:
return t
return None

def mro(self):
return (self,)

def dget(self, object, typeval):
return self

def dset(self, object, val):
return False

def call(self, *args, **kwargs):
raise TypeError("Not callable")

class Value(Object):
def __init__(self, val):
Object.__init__(self)
self.val = val

class Function(Object):
def __init__(self, fun):
Object.__init__(self)
self.fun = fun

def dget(self, object, typeval):
if object:
return BoundFunction(object, self)
else:
return self

def call(self, *args, **kwargs):
return self.fun(*args, **kwargs)

class BoundFunction(Object):
def __init__(self, obj, fun):
Object.__init__(self)
self.fun = fun
self.obj = obj

def call(self, *args, **kwargs):
return self.fun.call(self.obj, *args, **kwargs)

class Type(Object):
def __init__(self):
Object.__init__(self)

def call(self, *args, **kwargs):
obj = Object(self)
return obj

class Property(Object):
def __init__(self, get, set):
Object.__init__(self)
self.get = get
self.set = set

def dget(self, object, typeval):
if object:
return self.get.call(object)
else:
return self

def dset(self, object, val):
self.set.call(object, val)
return True

if __name__ == "__main__":
def addone(self, val):
return val + 1

def getfoo(self):
return self.dict["foo"]

def setfoo(self, val):
self.dict["foo"] = val

test = Type()
test.setattribute("b", Value(2))
test.setattribute("string", Value("foobar"))
test.setattribute("fun", Function(addone))
test.setattribute("getfoo", Function(getfoo))
test.setattribute("setfoo", Function(setfoo))
test.setattribute("foobar", Property(test.getattribute("getfoo"),
test.getattribute("setfoo")))

print test.getattribute("foobar")

inst = test.call()
inst.setattribute("foobar", Value(2))

print test.getattribute("foobar")
print inst.getattribute("foobar").val

inst.setattribute("b", Value(3))
print test.getattribute("b").val
print inst.getattribute("b").val

Jul 18 '05 #1
2 1890
Ben
Ben wrote:
Hi,

I was recently writing a python interpreter (in python), partly to teach
myself more about the python object system, and partly to learn more about
how programming languages work in general.
....


Arrgh, forgot to ask the question that was the entire point of the post

Is there any real difference between an unbound method, and a function,
apart from the scoping differences. I.e would it make any difference in the
get descriptor for a function, when called on a type, returned the function
instead of an unbound method?

Cheers

Ben
---

Jul 18 '05 #2
Ben <be*@transversal.com> wrote in message news:<3f***********************@news.easynet.co.uk >...

Arrgh, forgot to ask the question that was the entire point of the post

Is there any real difference between an unbound method, and a function,
apart from the scoping differences. I.e would it make any difference in the
get descriptor for a function, when called on a type, returned the function
instead of an unbound method?

Cheers

Ben
---


I do not really understand your question. Of course an unbound method
is different from a function, even if you can convert one in the other.
For instance, if you are inspecting code (for automatic generation of
documentation, or for metaprogramming purposes) the two things are
different: you get one with inspect.isfunction, the other with
inspect.ismethod. There are other differences too. Look at the source
code of the inspect module for more. I think you already know the standard
reference on descriptors:

http://users.rcn.com/python/download/Descriptor.htm
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Torsten Bronger | last post: by
10 posts views Thread by Matthew Wells | last post: by
reply views Thread by ZRexRider | last post: by
2 posts views Thread by Kevin Walzer | last post: by
14 posts views Thread by 7stud | last post: by
6 posts views Thread by Volker Neurath | last post: by
6 posts views Thread by Thomas Heller | last post: by

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.