473,394 Members | 1,965 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,394 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 980
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Berteun Damman | last post by:
Hello, First I was trying to get PyOSD, but as soon as I did `import pyosd' Python received a SIGABRT. Then I wrote my own module, which looks like: #include <Python.h> static PyMethodDef...
1
by: Jacob H | last post by:
Hello all, I would like to be able to take a module full of class instances, functions, etc and bind all its names to a separate container class in a different module. I have come up with the...
4
by: Benjamin Rutt | last post by:
I'm trying to learn about introspection in Python. my ultimate goal is to be able to build a module "text database" of all modules that are in the sys.path, by discovering all candidate modules...
2
by: Reid Priedhorsky | last post by:
Dear group, I'd have a class defined in one module, which descends from another class defined in a different module. I'd like the superclass to be able to access objects defined in the first...
59
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have thought of this and suggested it? Is this a...
2
by: Iouri | last post by:
I am using VS 2005 team edition. I cannot find Module in ASP.NET 2.0. It is not in the list of Visual Studio installed templates. Where is it? or what can I use to hold my global variables and...
3
by: Anton81 | last post by:
Hi, I want to use globals that are immediately visible in all modules. My attempts to use "global" haven't worked. Suggestions? Anton
3
by: Marco Aschwanden | last post by:
Hi I have a script that looks for modules and tries to load them. After loading I attach the "server"-API function to it. module = __import__(module_name, globals(), locals(), ) module.server...
6
by: pistacchio | last post by:
hi to all! can i load a module passing to it, automatically and as default, all the caller's global variables to act as module's global variables? thanks
11
by: Nadeem | last post by:
Hello all, I'm trying to write a function that will dynamically generate other functions via exec. I then want to be able to import the file (module) containing this function and use it in other...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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.