I have two classes;
a.py -->
#!/usr/bin/python
global test
test =''
class a(b):
def __init__(self,t est):
print test
print 'Outside: '+test
b.py -->
#!/usr/bin/python
import a
a.a('Garry')
I want to pass this string (or any object later) to a.py and that too
outside the scope of class a.
Is that possible??? If yes, how???
Thanks,
Garry 6 1400
If you create a.py like
-test = 'Spam'
-
-class a:
- def __init__(self, arg):
- global test
- test = arg
- self.result = arg + ' added'
- def __call__(self):
- return self.result
and b.py like
-import a
-a.test = 'donkey'
-x = a.a('dinosaur')
-print a.test
It will print 'dinosaur', so you changed the global variable. The thing
is however that this is a highly confusing way of programming for me,
so I would not use it just because it is possible
Hi
It would help if you could describe the purpose you have in mind for doing
this. There is a cute way of doing what you want:
===file: a.py===
# module a.py
test = 'first'
class aclass:
def __init__(self, mod, value):
mod.test = value # Is there another way to refer to
the module this class sits in?
===end: a.py===
===file: b.py===
# file b.py
import a
x = a.aclass(a,'mon key')
print a.test
===end: b.py===
When you run "b.py", you will see 'monkey' printed. What we have done
here is passed the reference to the *module* a to the constructor for
aclass, and that constructor modified the module variable "test". This is
a way to avoid using 'global', or in other words, the namespaces of things
are still clear (for me anyway).
Cya
Caleb
On Thu, 3 Feb 2005 00:13:05 +0530, Gurpreet Sachdeva
<gu************ ***@gmail.com> wrote: I have two classes;
a.py -->
#!/usr/bin/python global test test ='' class a(b): def __init__(self,t est): print test print 'Outside: '+test
b.py -->
#!/usr/bin/python import a a.a('Garry')
I want to pass this string (or any object later) to a.py and that too outside the scope of class a. Is that possible??? If yes, how???
Thanks,
Garry
Caleb Hattingh wrote: ===file: a.py=== # module a.py test = 'first' class aclass: def __init__(self, mod, value): mod.test = value # Is there another way to refer to the module this class sits in? ===end: a.py===
You can usually import the current module with:
__import__(__na me__)
so you could write the code like:
test = 'first'
class aclass:
def __init__(self, value):
mod = __import__(__na me__)
mod.test = value
or you could use globals() like:
test = 'first'
class aclass:
def __init__(self, value):
globals()['test'] = value
===file: b.py=== # file b.py import a x = a.aclass(a,'mon key') print a.test ===end: b.py===
If you used one of the solutions above, this code would be rewritten as:
import a
x = a.aclass('monke y')
print a.test
To the OP:
In general, this seems like a bad organization strategy for your code.
What is your actual use case?
Steve
Steven, thanks for your help once again :) so you could write the code like:
test = 'first' class aclass: def __init__(self, value): mod = __import__(__na me__) mod.test = value
This is sweet. I really like this technique for manipulating module-scope
identifiers (from within a class or function).
To the OP:
In general, this seems like a bad organization strategy for your code. What is your actual use case?
Agreed. It is an interesting intellectual exercise, but there is surely a
better way of controlling access to 'test' in the OP's post.
thx
Caleb
The purpose is, I pass a list to a class in a module but I want to use
that list out of the scope of that class and that too not in any other
class or a function but in the main program...
The problem is that when I import that, the statements in the module
which are not in the class are executed first and then the variable
gets intiallised...
I will explain with the example...
-global test
-
-class a:
- def __init__(self,t est):
- global test
- print test
-
-print 'Outside: '+test
I want to print that variable test which I am giving to the class as
an argument, in the scope of main...
I know it is not a good way of programming but my situation is like this...
But is this possible or not? If I pass test as 'Garry' can I (by any
way) print 'Outside: Garry' with that print statement... (in the main
scope)
Thanks and Regards,
Garry
Gurpreet Sachdeva wrote: The purpose is, I pass a list to a class in a module but I want to use that list out of the scope of that class and that too not in any other class or a function but in the main program... The problem is that when I import that, the statements in the module which are not in the class are executed first and then the variable gets intiallised... I will explain with the example...
-global test - -class a: - def __init__(self,t est): - global test - print test - -print 'Outside: '+test
I want to print that variable test which I am giving to the class as an argument, in the scope of main... I know it is not a good way of programming but my situation is like this... But is this possible or not? If I pass test as 'Garry' can I (by any way) print 'Outside: Garry' with that print statement... (in the main scope)
Probably a better approach for this would be something like:
class A(object):
def __init__(self, test):
self.test = test
def printtest(self) :
print 'Outside: %s' % self.test
where your code in your main module looks something like:
import amodule
a = amodule.A('Garr y')
a.printtest()
The __init__ method is used for initializing *instances* of a class, so
using the __init__ method to initialize a module-level variable doesn't
really make much sense. If you really want to play around with
module-level variables, you probably want to modify them with
module-level functions, e.g.:
test = 'default'
def a(val):
global test
test = val
def printtest():
print 'Outside: %s' % test
where your code in your main module looks something like:
import amodule
amodule.a('Garr y')
amodule.printte st()
Note that I've moved all your code that would normally be executed at
module import time into the 'printtest' function. This way you can
execute this code after import.
If you really need to have the code executed at the same time as the
module is imported, another option would be to exec your code in an
empty module instead of importing it:
py> a_module_str = """print 'Outside: %s' % test"""
py> a_module = new.module('a')
py> a_module.test = 'Garry'
py> exec a_module_str in a_module.__dict __
Outside: Garry
Here I create an empty module, set its 'test' attribute to 'Garry', and
then execute the rest of your code (e.g. the print statement). This is
certainly my least favorite approach, but it seems to do the closest
thing to what you're asking for.
Steve This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Eric Wilhelm |
last post by:
Hi,
By new-style classes, I'm referring to the changes which came into 2.2 as
a result of PEP 252 and 253. I discovered this problem when trying to
use the Perl Inline::Python module with a python class that was
inheriting from the new builting 'object' class like so:
class MyClass(object):
The problem is in detecting that this class should be treated as a class
|
by: Jean-François Doyon |
last post by:
Hello,
I'm using MetaClasses to create classes.
How do I make these new classes "globally" available?
I probably just have to assign them to something magic, but I can't seem to
figure out which one.
if I do:
|
by: David MacQuigg |
last post by:
I have what looks like a bug trying to generate new style classes with
a factory function.
class Animal(object): pass
class Mammal(Animal): pass
def newAnimal(bases=(Animal,), dict={}):
class C(object): pass
C.__bases__ = bases
dict = 0
|
by: Indiana Epilepsy and Child Neurology |
last post by:
Before asking this questions I've spent literally _years_ reading
(Meyer, Stroustrup, Holub), googling, asking more general design
questions, and just plain thinking about it. I am truly unable to
figure out what would be a "proper" OO design (in C++) for this.
There may be alternatives to writing my own ODBC handle classes, and I
may be interested in them, but I'd like to pursue this particular
problem, if for no other reason than to...
|
by: Robert |
last post by:
Hello.
I have been trying out the Lebans ToolTip Classes at
http://www.lebans.com/tooltip.htm, to display "balloon" style help tips in a
form. The classes I am using are located at
http://www.lebans.com/DownloadFiles/A2kTooltip.zip
So far the classes work perfectly, except that now I need to extend it to
support other controls besides the ones given in the example form. I have
gotten it to work with some controls, but not others. I...
| |
by: Frank Rizzo |
last post by:
We are having one of those religious debates at work: Interfaces vs
Classes. My take is that Classes give you more flexibility. You can
enforce a contract on the descendant classes by marking methods abstract
(which is all that an interface does). In addition, the classes allow
you to be flexible by adding functionality that child classes inherit.
Or by providing optional functionality via virtual methods.
Now, I understand that...
|
by: Edward Diener |
last post by:
Is the packing alignment of __nogc classes stored as part of the assembly ?
I think it must as the compiler, when referencing the assembly, could not
know how the original data is packed otherwise. Yet, in my understanding,
attributes are only __gc and __value class specific and do not apply to
__nogc classes. Is this correct ? If so, how is the packing alignment of
__nogc classes stored ?
|
by: Janaka Perera |
last post by:
Hi All,
We have done a object oriented design for a system which will create a
class multiply inherited by around 1000 small and medium sized
classes.
I would be greatful if you can help me with the following:
Can this create any problems with GNU g++ compilation, linking etc?
What would be the impact to the performance of the system?
Anything else we have to consider?
|
by: ademirzanetti |
last post by:
Hi there !!!
I would like to listen your opinions about inherit from a STL class
like list. For example, do you think it is a good approach if I
inherit from list to create something like "myList" as in the example
below ?
#include "Sector.h"
using namespace boost;
|
by: raylopez99 |
last post by:
Keywords: scope resolution, passing classes between parent and child
forms, parameter constructor method, normal constructor, default
constructor, forward reference, sharing classes between forms.
Here is a newbie mistake that I found myself doing (as a newbie), and
that even a master programmer, the guru of this forum, Jon Skeet,
missed! (He knows this I'm sure, but just didn't think this was my
problem; LOL, I am needling him)
If...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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,...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |