Re: Puzzling OO design problem



"Michael Spencer" <mahs@xxxxxxxxxxxxxxxxx> wrote:
>
> George,
>
> since you explicit allowed metaprogramming hacks :-), how about
something like
> this (not tested beyond what you see):
>
> [snipped]
>

Nice try, but ideally all boilerplate classes would rather be avoided
(at least being written explicitly). Also, it is not obvious in your
solution why and which placeholder classes have to be written (like
World2.Movable) and which do not. By the way, my current working
solution involves copying and pasting verbatim these classes :-) Below
is an abstracted example; note that the 'declaration string' of each
original class is exactly the same across all different versions after
the first (e.g. "class B(PreviousNamespace.B, A)").


#======================================================
# version_1.py

class Namespace:
class A(object):
def foo(self): return "version_1.foo()"
class B(A):
def bar(self): return "version_1.bar()"
class C(B):
def zen(self): return "version_1.zen()"
#======================================================
# version_2.py

from version_1 import Namespace as PreviousNamespace
class Namespace(PreviousNamespace):
class A(PreviousNamespace.A):
def foo(self): return "version_2.foo()"
class B(PreviousNamespace.B, A):
pass
class C(PreviousNamespace.C, B):
pass
#======================================================
# version_3.py

from version_2 import Namespace as PreviousNamespace
class Namespace(PreviousNamespace):
class A(PreviousNamespace.A):
pass
class B(PreviousNamespace.B, A):
def bar(self): return "version_3.bar()"
class C(PreviousNamespace.C, B):
pass

#======================================================
# test.py
# command: python test.py <#version>

def NamespaceFactory(version):
return __import__("version_%d" % version).Namespace

print NamespaceFactory(2).B().foo() # "version_2.foo()"
print NamespaceFactory(3).C().bar() # "version_3.bar()"

import sys, inspect
namespace = NamespaceFactory(int(sys.argv[1]))
# print the __mro__ of each 'inner' class
for name,cls in inspect.getmembers(namespace,
inspect.isclass):
print cls
for ancestor in cls.__mro__:
print "\t", ancestor

#======================================================

George

.



Relevant Pages

  • Re: I really hate .NET especially inside Delphi
    ... The question answers itself - you can't preserve the implicit make ... Since all the tooling around the language is predicated on ... packages, maybe, just maybe a move to explicit make logic would work. ... I can put that class in the namespace it logically belongs in, ...
    (borland.public.delphi.non-technical)
  • Re: Network namespaces a path to mergable code.
    ... patches just introducing namespaces for sockets in 2 ways: ... function parameters and using implicit current context. ... So I think the abstraction that we use to access per network namespace ... The explicit versus implicit lookup is just ...
    (Linux-Kernel)
  • Re: Designer generated code fails to compile
    ... I can tell you that you cannot get the tool to change it spots. ... is explicit should not matter. ... which is in this default namespace. ... How do I stop the designer from setting the namespace explicitly. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: I really hate .NET especially inside Delphi
    ... The question answers itself - you can't preserve the implicit make logic ... would the move to explicit make logic bring any ... explicit make system of a language (like Chrome, ... So when I write a class that extends a class in some other namespace, ...
    (borland.public.delphi.non-technical)
  • Re: partial class
    ... George, ... within the same assembly & an assembly needs to be compiled all at once. ... You can use either the System.CodeDom namespace or the ... | partial public class TheClass ...
    (microsoft.public.dotnet.languages.vb)