473,624 Members | 2,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

visibility between modules

hi.

if i have a single program file, different class instances can share
information in (at least) two fashions:

1. using instance variables:

class AClass:
def __init__(self):
self.att_1 = 42
self.att_2 = "Hello!"

class AnotherClass:
def __init__(self):
self.att_1 = anInstanceOfACl ass.att_1

anInstanceOfACl ass = AClass()
anInstanceOfAno therClass = AnotherClass()
print anInstanceOfAno therClass.att_1 ### This should print out 42

2. using globals:

class AClass:
def __init__(self):
self.att_1 = 42
self.att_2 = "Hello!"

class AnotherClass:
pass

aGlobalString = "No way."
anInstanceOfACl ass = AClass()
anInstanceOfACl ass.att2 = aGlobalString
anInstanceOfAno therClass = AnotherClass()
anInstanceOfAno therClass.att_1 = aGlobalString
print anInstanceOfACl ass.att2 ### This should output "No way."
print anInstanceOfAno therClass.att_1 ### And this too

----

i admit i prefer the fisrt way to do it. i have tried to make it work
even if the "main program" and each class definition reside in different
files, but i could not make it work:

ma**@172.17.1.2 01:/tmp$ cat AClass.py
class AClass:
def __init__(self):
self.att_1 = 42
self.att_2 = "Hello!"
ma**@172.17.1.2 01:/tmp$ cat AnotherClass.py
class AnotherClass:
def __init__(self):
self.att_1 = anInstanceOfACl ass.att_1
ma**@172.17.1.2 01:/tmp$ cat Main.py
from AClass import *
from AnotherClass import *
anInstanceOfACl ass = AClass()
anInstanceOfAno therClass = AnotherClass()
print anInstanceOfAno therClass.att_1 ### This should print out 42
ma**@172.17.1.2 01:/tmp$ python Main.py
Traceback (most recent call last):
File "Main.py", line 4, in ?
anInstanceOfAno therClass = AnotherClass()
File "/tmp/AnotherClass.py ", line 3, in __init__
self.att_1 = anInstanceOfACl ass.att_1
NameError: global name 'anInstanceOfAC lass' is not defined
ma**@172.17.1.2 01:/tmp$

----

any suggestion?

bye max
Jul 18 '05 #1
1 1347
"max(01)*" <ma**@fisso.cas a> writes:
hi.

if i have a single program file, different class instances can share
information in (at least) two fashions:

1. using instance variables:

class AClass:
def __init__(self):
self.att_1 = 42
self.att_2 = "Hello!"

class AnotherClass:
def __init__(self):
self.att_1 = anInstanceOfACl ass.att_1

anInstanceOfACl ass = AClass()
anInstanceOfAno therClass = AnotherClass()
print anInstanceOfAno therClass.att_1 ### This should print out 42

2. using globals:

class AClass:
def __init__(self):
self.att_1 = 42
self.att_2 = "Hello!"

class AnotherClass:
pass

aGlobalString = "No way."
anInstanceOfACl ass = AClass()
anInstanceOfACl ass.att2 = aGlobalString
anInstanceOfAno therClass = AnotherClass()
anInstanceOfAno therClass.att_1 = aGlobalString
print anInstanceOfACl ass.att2 ### This should output "No way."
print anInstanceOfAno therClass.att_1 ### And this too

Both these methods actually use globals. In ther first case, the
global is "anInstanceOfAC lass", that is bound to self.att_1 in the
__init__ method of AnotherClass.

The solution is to pass the instance in as a parameter:

class AClass:
def __init__(self):
self.att_1 = 42
self.att_2 = "Hello!"

class AnotherClass:
def __init__(self, instance):
sefl.att_1 = instance

anInstanceOfACl ass = AClass()
anInstanceOfAno therClass = AnotherClass(an InstanceOfAClas s)

or maybe just:

anInstaceOfAnot herClass = AnotherClass(AC lass())
<mike
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2661
by: jean-marc | last post by:
As an application programmer, I'm not well versed in the material aspects of computing (memory, cpu, bus and all). My understanding of imports in Python is such: the __main__ program is the center piece which holds the programs reference: globals, functions, classes, modules etc. The objects of this file (functions and classes) are directly accessible; 'import suchModule' s objects are attainable through the *qualified name*...
3
29673
by: Jukka K. Korpela | last post by:
I have noticed that the meaning of visibility: collapse has been discussed on different forums, but with no consensus on what it really means. Besides, implementations differ. The specification says: "The 'visibility' property takes the value 'collapse' for row, row group, column, and column group elements. This value causes the entire row or column to be removed from the display, and the space normally taken up by the row or column to...
3
1515
by: kele | last post by:
Do sub Namespaces have visibility to parent class. ie. I am trying to develop an application that is made up of modules and the main module contains a variables class that contains all the variables needed for the application. eg. MainModule Namespace Contains MainMod Class
12
3900
by: lawrence | last post by:
The following function correctly makes everything invisible but then fails to turn the one chosen DIV back to visible. I imagine I'm getting the syntax of the variable wrong? I've tried this with both single quotes and no single quotes, but it doesn't work either way. What am I doing wrong? <SCRIPT type='text/javascript'> function makeVisible(nameOfDiv) { document.getElementById('weblogs').style.visibility='hidden';
4
5463
by: lawrence | last post by:
Can anyone tell me why this code works in Netscape 7.1 but not in IE??? <SCRIPT type='text/javascript'> function makeVisible(nameOfDiv) { document.getElementById(nameOfDiv).style.visibility='visible'; document.getElementById(nameOfDiv).style.height='auto'; if (nameOfDiv != 'weblogs')
8
3367
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an actual program. To me "scope" of the name of a function or object are the general rules for the areas of a program that can through a declaration, have "visibility."
0
880
by: Sudharshan S | last post by:
Hi all, I have been learning to write extension modules using the C API that python provides, and have hit a minor roadblock that is turning out to be major headache. My project essentially is organized as follows, foo | --------------------------------- | | |
7
1276
by: saneman | last post by:
I have made two modules: 1) main.cpp Contains a main function and struct P. Includes head.h and std::vector #include "head.h" #include<vector> #include<iostream> struct P {
3
1818
by: Mohamed Yousef | last post by:
Hello , The problem I'm asking about is how can imported modules be aware of other imported modules so they don't have to re-import them (avoiding importing problems and Consicing code and imports ) Take Example :- in A.py :- import B print dir() # no problems we can see B which contain re module and C module
0
924
by: Terry Reedy | last post by:
Mohamed Yousef wrote: If you want to use module A in both B and C, B and C should both import A. No problem. ??
0
8681
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8629
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8341
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7170
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1793
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.