Two classes problem

  • Thread starter Gurpreet Sachdeva
  • Start date
G

Gurpreet Sachdeva

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
 
W

wittempj

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
 
S

Steven Bethard

Caleb said:
===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
 
C

Caleb Hattingh

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
 
C

Caleb Hattingh

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
 
G

Gurpreet Sachdeva

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
 
S

Steven Bethard

Gurpreet said:
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
 
C

Caleb Hattingh

Gurpreet

You can manage the namespace more formally. Or to put it another way,
"global" gives me the heebie-jeebies. I recently worked on a project
replacing a legacy reactor model in FORTRAN, and between COMMON blocks,
and GOTO statements, I didn't know up from down.

How about this:

***
class a:
def __init__(self,test):
localtest = test
# do stuff with localtest
def givetest(self):
return localtest
def printtest(self):
print localtest

test = 'Starting text'
print 'Outside before class: '+test
my_a = a(test)
test = my_a.givetest()
print 'Outside after class: '+test
***

So here we explicitly pass "test" into the class, do stuff with it, and
rewrite test again with a method. Does this satisfy the technical problem?

regards
Caleb
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,219
Messages
2,571,117
Members
47,730
Latest member
scavoli

Latest Threads

Top