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 2 1991
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
---
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Torsten Bronger |
last post by:
Hallöchen!
When my __del__ methods are called because the program is being
terminated, I experience difficulties in calling functions that I
need for a clean shutdown of my instances. So far,...
|
by: Matthew Wells |
last post by:
Hello.
I've converted a bound Access 2000 form which displays data retrieved from
an Access 2000 database to an unbound form. Now my hyperlinks don't work.
I'm assuming it's because the form...
|
by: ZRexRider |
last post by:
Hi,
I have a report driven by a SQL database. I have a populated recordset
having a user name and a number of data/time fields. I process these
time fields in the report's Detail_Print event. ...
|
by: Ron Garret |
last post by:
If I do this:
def f(self): print self
class c1: pass
setattr(c1, 'm1', f)
Then f is automagically transmogrified into the appropriate sort of
method depending on how it is used:
|
by: Kevin Walzer |
last post by:
I am trying to structure a Tkinter application with classes instead of
just with simple functions, but I'm not sure how to call methods from my
main class.
My main class is packetstreamApp()....
|
by: 7stud |
last post by:
Here is some example code that produces an error:
class Test(object):
def greet():
print "Hello"
t = Test()
t.greet()
TypeError: greet() takes no arguments (1 given)
|
by: Volker Neurath |
last post by:
Hi all,
I have a Problem with combobox-property "NotInList" and an unbound Form.
The situation:
On my main form i have three comboboxes for data-exchange (here: Names of
distributor,...
|
by: DeZZar |
last post by:
Hi all,
Unfortunately I am quite a novice with Access!! I've created a number
of data bases for my work however becuase my skills are limited to
really built in functionality and wizards my...
|
by: Thomas Heller |
last post by:
I'm currently using code like this to create unbound methods
from functions and stick them into classes:
method = new.instancemethod(raw_func, None, cls)
setattr(cls, name, method)
Ok, python...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |