Help creating new module which inherits existing class from anothermodule.

J

Jonno

I'm not sure if this list is a suitable place to ask for this kind of help
so if it's not please just suggest another forum which might be more
suitable.

I'm looking for help/suggestions how to architect a module (perhaps just a
class).

There is an existing module I want to use which has a class we'll call
*Existing
Class*.

I want to create a python module which allows me to create *new_objects* with
the following properties:

- The new_objects have all the attributes of the Existing_Class (simply
create a class that inherits from Existing_Class)
- I then want to create a nested structure under the new_objects
something like:

new_object.foo

new_object.foo.bar

new_object.foo.bar.baz

Where foo, bar, baz have the following properties:

- All have *docstrings*
- All are available for *tab completion* tools upon new_object creation.
-

Some of which will have *new methods* which act in the following way:
- new_object.foo.bar()

calls
- new_object.existing_method("foo.bar", *args)

I believe I'll need to use metaclasses and have an idea that
types.MethodType will help but I keep getting stuck. Any pointers would be
greatly appreciated.
 
S

Steven D'Aprano

There is an existing module I want to use which has a class we'll call
*Existing Class*.

I want to create a python module which allows me to create *new_objects*
with the following properties:

- The new_objects have all the attributes of the Existing_Class
(simply create a class that inherits from Existing_Class)

That part is easy:

from some_module import ExistingClass

class NewClass(ExistingClass):
pass

newobject = NewClass()

- I then
want to create a nested structure under the new_objects something
like:

new_object.foo

new_object.foo.bar

new_object.foo.bar.baz

Where foo, bar, baz have the following properties:

- All have *docstrings*
- All are available for *tab completion* tools upon new_object
creation.

Well, that depends on the tab completion tools you are using.

- Some of which will have *new methods* which act in the following
way:
- new_object.foo.bar()

calls
- new_object.existing_method("foo.bar", *args)


Seems awfully complicated, and I don't understand what you would do with
these strange things, but okay.

Start by ignoring the "existing class" and concentrated on these foo,
foo.bar, foo.bar.baz things. It isn't clear to me what they are, but I
think that you want foo to be an object which is callable, as well as
having an attribute bar (which itself is callable, etc.). The way to do
that is with a class:

class FooClass:
def __init__(self, parent):
self.parent = parent

def __call__(self):
return self.parent.existing_method("something")

Add your foo, bar and baz methods as needed, and we'll look for a way to
calculate "something" on the fly later.


Now remember your NewClass definition above? What we'd like to do is this:

class NewClass(ExistingClass):
foo = FooClass(self)

but of course you can't, because self doesn't exist until the class is
instantiated! So what we need if for foo to be a descriptor. I don't
quite remember the descriptor syntax, so this may be wrong, but I think
you want something like this:

# This is probably wrong.
class FooDescriptor(object):
def __get__(self, cls, obj):
return FooClass(obj)

and then in NewClass you put in:

foo = FooDescriptor()


The idea is that when you have a NewClass instance, calling
"newobject.foo" will automatically call the descriptor's __getmethod__,
passing it the class and instance. That descriptor will create and
populate the FooClass instance, which does the real work.

Descriptors are how methods, classmethods, staticmethods and properties
work, so they are a fundamental, powerful way of implementing things like
this.
 
J

Jonno

The idea is that when you have a NewClass instance, calling
"newobject.foo" will automatically call the descriptor's __getmethod__,
passing it the class and instance. That descriptor will create and
populate the FooClass instance, which does the real work.

Descriptors are how methods, classmethods, staticmethods and properties
work, so they are a fundamental, powerful way of implementing things like
this.

Thanks for the suggestion Steven. Hopefully my last email on the subject
helped explain the intention a little better.
I'll look into descriptors some more. I had the feeling I needed to use one
(or more) of the following:
descriptors
decorators
metaclasses
None of which I know much about yet. I will do some reading & test out your
suggestion. Thanks again.
 
B

biolord9

I'm not sure if this list is a suitable place to ask for this kind of help so if it's not please just suggest another forum which might be more suitable.




I'm looking for help/suggestions how to architect a module (perhaps just a class).

There is an existing module I want to use which has a class we'll call Existing Class.

I want to create a python module which allows me to create new_objects with the following properties:
The new_objects have all the attributes of the Existing_Class (simply create a class that inherits from Existing_Class)
I then want to create a nested structure under the new_objects something like:

new_object.foo


new_object.foo.bar


new_object.foo.bar.baz


Where foo, bar, baz have the following properties:
All have docstrings
All are available for tab completion tools upon new_object creation.

Some of which will have new methods which act in the following way:

new_object.foo.bar()

calls
new_object.existing_method("foo.bar", *args)


I believe I'll need to use metaclasses and have an idea that types.MethodType will help but I keep getting stuck. Any pointers would be greatly appreciated.
===============
BREAKING NEWS!
===============
NEW YORK TIMES, THRINAXODON, OHIO
=====================THRINAXODON RECENTLY FOUND 3 HUMAN FOSSILS FROM DEVONIAN STRATA FROM GREENLAND, THE EVOLUTIONISTS HAVE NO BONES ABOUT.ONE EVIL EVOLUTIONIST, BOB CASANOVA HAS ADMITTED THAT HUMAN EVOLUTION IS INFREE-FALL.RICHARD LEAKEY HAS DIED FROM A HEART ATTACK DUE TO THIS GROUND-BREAKING FIND THAT CONCLUSIVELY SHOWS THAT HUMANS HAVE ORIGINS IN THE DEVONIAN.NOW, IF YOU PLEASE, I HAVE TO SUE THE SMITHSONIAN FOR YEARS OF CENSORSHIP.======================================
EVIDENCE THAT HUMANS LIVED IN THE DEVONIAN:

https://groups.google.com/group/sci.bio.paleontology/browse_thread/thread/6f501c469c7af24f#

https://groups.google.com/group/sci.bio.paleontology/browse_thread/thread/3aad75c16afb0b82#
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top