Re: Python-specific question: variable scope
It's not strongly related to some specific problem, but more to the better python perception and self-training. Thank you all for your time and attention. Thank you, Jerremy D, for the interesting solutions. Regards, Aidas Bendoraitis [aka Archatas] On 3/31/07, Forest Bond <[EMAIL PROTECTED]> wrote: > On Fri, Mar 30, 2007 at 08:07:51PM +0200, Aidas Bendoraitis wrote: > > > > Actually the was no circular import since I haven't imported a from b, > > but just b from a. > > Sorry, mis-read your code, I thought there were only two modules. > > > And unfortunately your answer didn't solve the original question, > > which is getting the value from the module that imports the current > > module. > > Pardon. I mis-understood that, too. > > So, your goal is to make Python imports act more like PHPincludes? Wny on > earth > would you want to do that? There is no global namespace in Python, and for > good > reason. What are you really trying to achieve? > > -Forest > > -BEGIN PGP SIGNATURE- > Version: GnuPG v1.4.3 (GNU/Linux) > > iD8DBQFGDZE/RO4fQQdv5AwRAh4xAJ4mgzleRFvarzodA3jkkZnoj9330QCgsjmq > fZCZMl5LQDOvCub8KiDqdzY= > =cNIV > -END PGP SIGNATURE- > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Python-specific question: variable scope
On Fri, Mar 30, 2007 at 08:07:51PM +0200, Aidas Bendoraitis wrote: > > Actually the was no circular import since I haven't imported a from b, > but just b from a. Sorry, mis-read your code, I thought there were only two modules. > And unfortunately your answer didn't solve the original question, > which is getting the value from the module that imports the current > module. Pardon. I mis-understood that, too. So, your goal is to make Python imports act more like PHPincludes? Wny on earth would you want to do that? There is no global namespace in Python, and for good reason. What are you really trying to achieve? -Forest signature.asc Description: Digital signature
Re: Python-specific question: variable scope
Here's a few ideas: Solution 1 - use the __main__ module The downside to this is that it always reads x from the "top-level" module, in other words, the script that is being run. So when you run a1, you'll get "a1" when you run a2 module b will then find "a2". a1.py -- import b x = "a1" b.test() -- a2.py -- import b x = "a2" b.test() -- b.py -- import __main__ def test(): print __main__.x -- I'm not sure why you're trying to do this, but it looks like having b reference variable x which is implied to have been defined elsewhere could cause trouble for you later. x is sort of like a parameter to make b work differently, so you should probably explicitly pass it to functions in b or use x to initialize an object of a class from b. Idea 2 - use a class to encapsulate x If you wrap up all the functionality you need in a class inside b, you could then pass x to the constructor and all the subsequent method calls would have access to x. a1.py -- import b x = "a1" my_b = b.B(x) my_b.test() -- a2.py -- import b x = "a2" my_b = b.B(x) my_b.test() -- b.py -- class B(object): def __init__(self, x): self.x = x def test(self): print self.x -- Solution 3 - Use the borg pattern If you need all modules everywhere to see b with the same value of x, you could use the borg pattern (singleton would work too, but I like borg better). This way the first a module that gets imported or run will set b's value of x forever. a1.py -- import b x = "a1" my_b = b.B(x) my_b.test() -- a2.py -- import b x = "a2" my_b = b.B(x) my_b.test() -- a3.py - note that this one will print "a1" twice, since module a1 initializes b before a3 can. -- import a1 import b x = "a3" my_b = b.B(x) my_b.test() -- b.py -- class B(object): borg = None def __init__(self, x): if B.borg is None: B.borg = {} B.borg['x'] = x self.__dict__ = B.borg def test(self): print self.x -- --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Python-specific question: variable scope
On 3/30/07, Jeremy Dunck <[EMAIL PROTECTED]> wrote: ... > In general, no. Python is lexically scoped, so that when b.test is > called, it checks the scope of test, then b, then __builtins__, then > fails with NameError. Good intro to scoping, if needed: http://en.wikipedia.org/wiki/Dynamic_scoping --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Python-specific question: variable scope
On 3/30/07, Aidas Bendoraitis <[EMAIL PROTECTED]> wrote: ... > b.py: > --- > def test(): > print x > > -- > Is it possible to access the x of the module a.py in the module b.py? > What would be the functions/statements to make it possible? Or in > general, how to access the locals of the importing ( not imported!!!) > module? In general, no. Python is lexically scoped, so that when b.test is called, it checks the scope of test, then b, then __builtins__, then fails with NameError. You -can- do some evil hacking of the call stack, but this is highly unusual, and I think you really want fluid/dynamic scoping anyway. Hugo wrote a codelib for this a while ago. It (usually) lives here: https://simon.bofh.ms/cgi-bin/trac-django-projects.cgi/wiki but that server seems to be down right now. Basically, the API is something like this: main.py from fluid import scope import a scope.x = 1 a.b.test() - a.py -- import b b.py -- from fluid import scope def test(): print scope.x # -> 1 -- A poor man's system is just to maintain the scope stack yourself, though I think a system of decorators are stack inspection upon attribute lookup could make the API nicer. ... I recall Hugo's being very nice; I hope it's back up soon. There are other nice toys on that server. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Python-specific question: variable scope
Maybe this leads to nothing and is more-or-less a theoretical question, but anyway, I'll give you another example: a1.py x = "A1" import b a2.py x = "A2" import b b.py - # the module which imports me is a blackbox to me def get_the_x_of_the_module_which_is_importing_me(): return #??? Regards, Aidas Bendoraitis [aka Archatas] P.S. If I am not understandable because of "strange" English, it might be that I need some coffee or sleep. :) On 3/30/07, Rubic <[EMAIL PROTECTED]> wrote: > > b.py: > > import a > ... > def test(): > print a.x > > -- > Jeff Bauer > Rubicon, Inc. > > > On Mar 30, 6:43 am, "Aidas Bendoraitis" <[EMAIL PROTECTED]> > wrote: > > Let's say I have the files main.py, a.py and b.py > > > > main.py: > > --- > > x="some local value" > > import a > > ... > > > > a.py: > > --- > > x="some other local value" > > import b > > ... > > > > b.py: > > --- > > def test(): > > print x > > > > -- > > Is it possible to access the x of the module a.py in the module b.py? > > What would be the functions/statements to make it possible? Or in > > general, how to access the locals of the importing ( not imported!!!) > > module? > > > > Regards, > > Aidas Bendoraitis > > > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Python-specific question: variable scope
b.py: import a ... def test(): print a.x -- Jeff Bauer Rubicon, Inc. On Mar 30, 6:43 am, "Aidas Bendoraitis" <[EMAIL PROTECTED]> wrote: > Let's say I have the files main.py, a.py and b.py > > main.py: > --- > x="some local value" > import a > ... > > a.py: > --- > x="some other local value" > import b > ... > > b.py: > --- > def test(): > print x > > -- > Is it possible to access the x of the module a.py in the module b.py? > What would be the functions/statements to make it possible? Or in > general, how to access the locals of the importing ( not imported!!!) > module? > > Regards, > Aidas Bendoraitis --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Python-specific question: variable scope
Actually the was no circular import since I haven't imported a from b, but just b from a. And unfortunately your answer didn't solve the original question, which is getting the value from the module that imports the current module. Or maybe the is a way to include another file and parse it with the context of the current one, like include statement in PHP? If there are no ways to do that, then I will just use some workarounds. At first, I am interested about possibilities. Regards, Aidas Bendoraitis aka Archatas On 3/30/07, Forest Bond <[EMAIL PROTECTED]> wrote: > On Fri, Mar 30, 2007 at 01:43:41PM +0200, Aidas Bendoraitis wrote: > > > > Let's say I have the files main.py, a.py and b.py > > > > main.py: > > --- > > x="some local value" > > import a > > ... > > > > > > a.py: > > --- > > x="some other local value" > > import b > > ... > > > > > > b.py: > > --- > > def test(): > > print x > > > > -- > > Is it possible to access the x of the module a.py in the module b.py? > > What would be the functions/statements to make it possible? Or in > > general, how to access the locals of the importing ( not imported!!!) > > module? > > Well, you have a circular import here that may cause you problems. Neither > module could finish loading because it depends on the other being imported > which > depends on the other being imported which depends on the other being imported > > > But, this could work: > > a.py: > > x = 'some local value' > > > b.py: > > x = 'some other local value' > > def print_a_x(): > import a > print a.x # prints 'some local value' > > def print_x(): > print x # prints 'some other local value' > > > Hope this helps. > > -Forest > > -BEGIN PGP SIGNATURE- > Version: GnuPG v1.4.3 (GNU/Linux) > > iD8DBQFGDPn2RO4fQQdv5AwRApiIAJ9lqLzefjFU8aZMWYunM7aLehOUAwCfV+UU > 0HQMbDvTGxLvi0phpgn0XOU= > =Pt2E > -END PGP SIGNATURE- > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Python-specific question: variable scope
On Fri, Mar 30, 2007 at 01:43:41PM +0200, Aidas Bendoraitis wrote: > > Let's say I have the files main.py, a.py and b.py > > main.py: > --- > x="some local value" > import a > ... > > > a.py: > --- > x="some other local value" > import b > ... > > > b.py: > --- > def test(): > print x > > -- > Is it possible to access the x of the module a.py in the module b.py? > What would be the functions/statements to make it possible? Or in > general, how to access the locals of the importing ( not imported!!!) > module? Well, you have a circular import here that may cause you problems. Neither module could finish loading because it depends on the other being imported which depends on the other being imported which depends on the other being imported But, this could work: a.py: x = 'some local value' b.py: x = 'some other local value' def print_a_x(): import a print a.x # prints 'some local value' def print_x(): print x # prints 'some other local value' Hope this helps. -Forest signature.asc Description: Digital signature
Python-specific question: variable scope
Let's say I have the files main.py, a.py and b.py main.py: --- x="some local value" import a ... a.py: --- x="some other local value" import b ... b.py: --- def test(): print x -- Is it possible to access the x of the module a.py in the module b.py? What would be the functions/statements to make it possible? Or in general, how to access the locals of the importing ( not imported!!!) module? Regards, Aidas Bendoraitis --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---