Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 18th, 2005, 10:00 PM
Gurpreet Sachdeva
Guest
 
Posts: n/a
Default 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
  #2  
Old July 18th, 2005, 10:00 PM
wittempj@hotmail.com
Guest
 
Posts: n/a
Default Re: Two classes problem

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

  #3  
Old July 18th, 2005, 10:00 PM
Caleb Hattingh
Guest
 
Posts: n/a
Default Re: Two classes problem

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
<gurpreet.sachdeva@gmail.com> wrote:
[color=blue]
> 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[/color]

  #4  
Old July 18th, 2005, 10:00 PM
Steven Bethard
Guest
 
Posts: n/a
Default Re: Two classes problem

Caleb Hattingh wrote:[color=blue]
> ===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===[/color]

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
[color=blue]
> ===file: b.py===
> # file b.py
> import a
> x = a.aclass(a,'monkey')
> print a.test
> ===end: b.py===[/color]

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
  #5  
Old July 18th, 2005, 10:00 PM
Caleb Hattingh
Guest
 
Posts: n/a
Default Re: Two classes problem

Steven, thanks for your help once again :)
[color=blue]
> so you could write the code like:
>
> test = 'first'
> class aclass:
> def __init__(self, value):
> mod = __import__(__name__)
> mod.test = value[/color]

This is sweet. I really like this technique for manipulating module-scope
identifiers (from within a class or function).
[color=blue]
> To the OP:
>
> In general, this seems like a bad organization strategy for your code.
> What is your actual use case?[/color]

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
  #6  
Old July 18th, 2005, 10:01 PM
Gurpreet Sachdeva
Guest
 
Posts: n/a
Default Re: Two classes problem

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
  #7  
Old July 18th, 2005, 10:01 PM
Steven Bethard
Guest
 
Posts: n/a
Default Re: Two classes problem

Gurpreet Sachdeva wrote:[color=blue]
> 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)[/color]

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
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles