471,321 Members | 2,097 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,321 software developers and data experts.

how to get module globals into a class ?

hello,

this question may look a little weird,
but I want to create library shells that are a simple as possible.

So I've a module where one base class is defined,
which looks like this (and might be complex)

base_class_file.py
class brick_base ( object ) :
....

now I've a lot of library files,
in each library file are a lot of classes,
and each library-file, has some specific parameters, like "library_color",
so something like this:

library_file.py
library_color = ...

class brick_do_something1( brick_base ) :
init :
self.Library_Color = Library_Color
....

class brick_do_something2( brick_base ) :
init :
self.Library_Color = Library_Color
....

Now this works fine, ...
.... but the statement "self.Library_Color = Library_Color"
is completely redundant, because it should be in every class of every
librray file.
So I would like to move this statement to the base-class-file,
but I can't figure out how to accomplish that.

thanks,
Stef Mientki
Dec 9 '07 #1
1 941
stef mientki wrote:
hello,

this question may look a little weird,
but I want to create library shells that are a simple as possible.

So I've a module where one base class is defined,
which looks like this (and might be complex)

base_class_file.py
class brick_base ( object ) :
....

now I've a lot of library files,
in each library file are a lot of classes,
and each library-file, has some specific parameters, like "library_color",
so something like this:

library_file.py
library_color = ...

class brick_do_something1( brick_base ) :
init :
self.Library_Color = Library_Color
....

class brick_do_something2( brick_base ) :
init :
self.Library_Color = Library_Color
....

Now this works fine, ...
... but the statement "self.Library_Color = Library_Color"
is completely redundant, because it should be in every class of every
librray file.
So I would like to move this statement to the base-class-file,
but I can't figure out how to accomplish that.
You can use a hack like

class Base(object):
def __init__(self):
module = __import__(self.__module__)
self.library_color = module.library_color

But I think it's better to add another level to the class hierarchy:

class BrickBase(object):
def __init__(self):
self.library_color = self.library_color

class LibraryBase(BrickBase):
library_color = "blue"

class BrickDoSomethingN(LibraryBase):
pass

(all untested)

Peter
Dec 10 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Berteun Damman | last post: by
1 post views Thread by Jacob H | last post: by
4 posts views Thread by Benjamin Rutt | last post: by
2 posts views Thread by Reid Priedhorsky | last post: by
2 posts views Thread by Iouri | last post: by
3 posts views Thread by Anton81 | last post: by
3 posts views Thread by Marco Aschwanden | last post: by
6 posts views Thread by pistacchio | last post: by

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.