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

Further questions on dictionaries, namespaces, etc.

Thanks to everyone for pointing out my bone-headed errors :) I don't
*usually* make that sort of mistake...

What I'm attempting to do is create an implentation of the unification
algorithm. Now, its fairly straightforward to do unification on my own
custom types, all I have to do is define a unify() method for each class:

def unify( self, other, bindings ):
...

However, this doesn't work so well when the objects being unified are
built-in python types (tuples, integers, etc.) This requires the
"un-pythonic" testing of each individual type and then calling the
appropriate unification function for the given type.

Another thing that is important is that this particular unifier supports
commutative functions (which accept their arguments in any order), which
is what the permutation stuff is for. (I haven't done associativity yet,
still working on it.) The unifier is structured as a stack of
generators, each returning all possible matches. This allows the higher
levels of the unifier to backtrack by processing alternatives produced
by the lower-level generators.

So here's my list of further questions:

1) Is there are better way to do "functional overloading" on built-in
types than using a whole series of "if type( x ) is y".

2) Is there an easy way to determine if a given object has a callable
method named "unify"? I know how to determine if there's an attribute
with a name, but not how to determine whether or not that attribute is
callable.

3) The "bindings" object is a dictionary that is constructed as the
unification proceeds. (So for example, if I attempt to unify "x + 1"
with "2 + 1" it will have the binding "x : 2" in the dictionary.

I want this bindings object to behave like a function call scope - in
that you can "see" the variables in the enclosing scope. In fact, I'd
like to be able to use a regular python namespace (such as a module) as
the enclosing scope, so that the unification algorithm has access to all
of the variable definitions within that module. (Again, this is part of
the notion that I want to be able to do unification operations on normal
python data structures, rather than specially "wrapped" types.)

In fact, I had thought about the idea of the bindings being an actual
Python "module" object, however that doesn't appear to be possible (or
what I want for that matter.) Similarly, being able to take the local
function scope and capture it in a closure and export that to the
outside world was also something I briefly pondered.

Because of the backtracking nature of the matcher, I need to be able to
"undefine" bindings that have been previously defined. The way I am
currently doing this is to create a new "scope" each time I add a new
binding, where the new scope's "parent" is the previous scope. (So in
fact my dictionary has only one entry in it.) Something like this:

for new_bindings in generate_matches( a, b, old_bindings ):
...

If none of the alternatives pan out, it simply discards "new_bindings"
and reverts back to the old_bindings.

So my question here is: Do I want to continue using a subclass of dict
for this, or something more exotic?

4) I've seen a couple of code examples on the net where people use the
idiom "lambda x: (for x in [])" to represent a "null" iterator, i.e. one
that immediately terminates. How is this any different from simply
returning "()"? (Or range( n, n ) for that matter.) Which one is the
most efficient? And if they are different, how about adding a null
iterator to itertools :)

-- Talin

Aug 21 '05 #1
1 1162
Talin a écrit :
(snip)
2) Is there an easy way to determine if a given object has a callable
method named "unify"?


if callable(getattr(obj, 'unify', None)):
# obj.unify exists and is callable

Br
Aug 21 '05 #2

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

Similar topics

7
by: Elaine Jackson | last post by:
Two quick newbie questions: 1) Does Python have passing-by-reference? 2) In ordinary parlance, "deep" implies "shallow" but not conversely. In the Python "copy" module (if I understand...
1
by: Limey | last post by:
Hi All, I want to write an application that will talk to a RDBMS. The application needs to be platform neutral from both an operating system and database backend point of view. I have...
1
by: Tony Johansson | last post by:
Hello I'm trying to learn XML by reading a book. There is some text in the book that I don't understand so I try to ask you out there. My first question is does this statement mean that ...
3
by: Faisal Alquaddoomi | last post by:
Hello, I'm having a bit of trouble isolating my scripts from each other in my embedded Python interpreter, so that their global namespaces don't get all entangled. I've had some luck with...
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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...
0
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,...
0
jinu1996
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...
0
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...

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.