How to build Hierarchies of dict's? (Prototypes in Python?)

Charles D Hixson charleshixsn at earthlink.net
Sat Feb 24 19:29:13 EST 2007


Toby wrote:
> Charles D Hixson wrote:
>   
>> What I basically want is a kind of class that has both class and 
>> instance level dict variables, such that descendant classes 
>> automatically create their own class and instance level dict variables.  
>> The idea is that if a member of this hierarchy looks up something in 
>> it's local dict, and doesn't find it, it then looks in the class dict, 
>> and if not there it looks in its ancestral dict's.  This is rather like 
>> what Python does at compile time, but I want to do it at run time.
>>     
>
> I don't understand, Python already does it at runtime:
>
> class A:
>   A_class_var = 1
>
> class B(A):
>   B_class_var = 2
>   def __init__(self):
>     self.B_inst_var = 3
>
>   
>>>> b.A_class_var
>>>>         
> 1
>   
>>>> b.B_class_var
>>>>         
> 2
>   
>>>> b.B_inst_var
>>>>         
> 3
>   
>>>> A.another = 4
>>>> b.another
>>>>         
> 4
>
> Can you post a ">>>"-script of what you would like your classes to do?
>
>
> Toby
>   
Sorry, the "script" hasn't been written.  But Python apparently *won't* 
(automatically) do what I want, which is create a class whose 
sub-classes automatically have unique class variables of a determined 
form such that I can do a hierarchical search through them.  (I could 
plausibly handle this for the instance case but "class variables are 
static", so it looks like I can't do it for class variables in a 
straightforward manner.  This looks like it's going to demand a factory 
method, or some alternate approach.  (Not sure yet which I'll choose.)

What I'm looking for is a reasonable way to implement what Marvin Minsky 
calls Panologies.  These are "objects" (my term) with sufficient local 
intelligence to try alternative models of a situation until they find 
one that's appropriate.  E.g., is this being operated on as a physical 
transaction or a social transaction.  (Yes, it is physically happening, 
and it's taking place in physical space, but different models yield 
different analyses of what's happening.  Which is appropriate for the 
situation currently being evaluated?)

Well, I'm not trying to handle all that, merely to create a simple model 
of what a panology *IS*.  So each Panology object will need a list of 
delegatees to handle the operations, and a list of indexes to assist it 
in evaluating which delegatee to use.  If it doesn't find it locally, it 
will need to search through the situational context and these should be 
maintained at both the class and instance levels by it's ancestors.  The 
base class should have a method that manages the searching, and the 
finding of delegatees (models) based around the indexing keys.

This will take a bit of work, but if I can manage it, it should be 
rather interesting.  It could also be used to test some of Minsky's 
ideas...or not, because what I'm thinking of it different from exactly 
what he's proposing.

E.g., after I figure out how to build the basic structure, I'm going to 
need to figure out how to determine the appropriate model.  This may be 
a harder problem.

P.S.:  What I really expect is that by the time I get this solved, 
somebody else will already have a solution.  (It's happened before.  I 
had the "idea" of an "intelligent spam filter" before spambayes was 
built...but I was much too slow at the implementation.  MUCH!  (I spend 
too much time dreaming and not enough time programming.)

P.P.S.:  This isn't all loss.  I'm expecting that eventually I'll start 
running into performance problems, and at that point it would be 
necessary to start translating into a native-compiler language.



More information about the Python-list mailing list