473,378 Members | 1,721 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,378 software developers and data experts.

Two classes problem

I have two classes;

a.py -->

#!/usr/bin/python
global test
test =''
class a(b):
def __init__(self,test):
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
Jul 18 '05 #1
6 1368
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

Jul 18 '05 #2
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,'monkey')
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,test):
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


Jul 18 '05 #3
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__(__name__)

so you could write the code like:

test = 'first'
class aclass:
def __init__(self, value):
mod = __import__(__name__)
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,'monkey')
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('monkey')
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
Jul 18 '05 #4
Steven, thanks for your help once again :)
so you could write the code like:

test = 'first'
class aclass:
def __init__(self, value):
mod = __import__(__name__)
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
Jul 18 '05 #5
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,test):
- 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
Jul 18 '05 #6
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,test):
- 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('Garry')
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('Garry')
amodule.printtest()

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
Jul 18 '05 #7

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

Similar topics

1
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...
13
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...
12
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={}):...
2
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...
6
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...
30
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...
18
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....
12
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...
7
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...
12
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....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.