Re: namespace issue, Python vs numpy min/max problem
Steven D'Aprano wrote: It only becomes your problem if you have advised people that the right way to use your module is with import *. And if you're advising people to do that, it would be an extremely good idea to give your functions different names so that they don't conflict with the builtin min and max. -- Greg -- http://mail.python.org/mailman/listinfo/python-list
Re: namespace issue, Python vs numpy min/max problem
On 2010-11-14 17:37 , Gregory Ewing wrote: Steven D'Aprano wrote: It only becomes your problem if you have advised people that the right way to use your module is with import *. And if you're advising people to do that, it would be an extremely good idea to give your functions different names so that they don't conflict with the builtin min and max. And we (the numpy devs) explicitly advise people *against* that. -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: namespace issue, Python vs numpy min/max problem
Well, I think I have found an appropriate solution. Regards, D. -- http://mail.python.org/mailman/listinfo/python-list
Re: namespace issue, Python vs numpy min/max problem
On 13/11/2010 19:55, dmitrey wrote: Well, I think I have found an appropriate solution. Regards, D. Hi Dmitrey, Would you mind briefly describing your solution? Thanks, Ben -- http://mail.python.org/mailman/listinfo/python-list
Re: namespace issue, Python vs numpy min/max problem
On 11/13/2010 2:41 PM, dmitrey wrote: hi all, I have the following problem: I have overloaded max function in my module (FuncDesigner); it works like following: if some data in arguments is of type oofun then my function works, elseware numpy.max() is used. Now the problem: suppose someone writes from FuncDesigner import * Noone should do that unless your module is **explicitly** designed to be *-imported and you docuement that so that they know it is OK. Now you know why * import is generally discouraged ;-). -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: namespace issue, Python vs numpy min/max problem
On Sat, 13 Nov 2010 11:41:09 -0800, dmitrey wrote: hi all, I have the following problem: I have overloaded max function in my module (FuncDesigner); it works like following: if some data in arguments is of type oofun then my function works, elseware numpy.max() is used. Now the problem: suppose someone writes from FuncDesigner import * ... a = max(0,1) so it calls FuncDesigner, it calls numpy.max and result is 0 (because in numpy it means max of array with single element 0 along 1st axis). But if user has not imported numpy then he expected calling ordinary Python max and thus result to be 1. That's not your problem, that's the caller's problem. He shouldn't be calling import * unless he wants to import everything in your module directly into his namespace. If the caller said to you: After executing len = x, I can't access the built-in len function any more, can you change x so that it magically tells when I want the built- in behaviour and does that? you'd probably tell him to get lost. Doing import * is no different. It only becomes your problem if you have advised people that the right way to use your module is with import *. Is there any way to get rid of the problem (somehow automatically determine which func should be used - numpy or Python max)? The same issue with min, but they are equivalent, of course. Automatically? No. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Namespace issue
On May 23, 1:20 pm, Ritesh Raj Sarraf [EMAIL PROTECTED] wrote: Hi, I need a little help in understanding how Namespaces and scoping works with Classes/Functions in Python. Here's my code: class FetchData: def __init__(self, dataTypes=[foo, bar, spam], archive=False): self.List = [] self.Types = dataTypes if archive: self.Archiver = Archiver(True) def FetchData(self, PackageName, Filename=None): try: import my_module except ImportError: return False if Filename != None: try: file_handle = open(Filename, 'a') except IOError: sys.exit(1) (amnt, header, self.List) = my_module.get_data(PackageName) This is the only way this code will work. As per my understanding, the bad part is that on every call of the method FetchData(), an import would be done. To not let that happen, I can put the import into __init__(). But when I put in there, I get a NameError saying that my_module is not available even though it got imported. All I noticed is that the import has to be part of the method else I end up getting a NameError. But always importing my_module is also not good. What is the correct way of doing this ? IMO, ideally it should be part of __init__() and be imported only when the class is instantiated. Thanks, Ritesh -- If possible, Please CC me when replying. I'm not subscribed to the list. The reason you can't put the import into the __init__ is that that is also a method, so the imported module is only available to that method's namespace. This is also true if you had put the import into any other method. The only way to make it global is to put your class into its own module and put the import before the class creation. Depending on how your instantiating the class, you may not need to worry about the whole importing thing. If you have all your code like this: class something(obj): def __init__(self): #do something def grabData(self): # do something import module x = something() y = something() Then the module gets imported only once. See http://www.python.org/search/hypermail/python-1993/0342.html I'm not sure what happens when you have one module calling another module that import a third module though. Mike -- http://mail.python.org/mailman/listinfo/python-list
Re: Namespace issue
On May 23, 1:20 pm, Ritesh Raj Sarraf [EMAIL PROTECTED] wrote: Hi, I need a little help in understanding how Namespaces and scoping works with Classes/Functions in Python. Here's my code: class FetchData: def __init__(self, dataTypes=[foo, bar, spam], archive=False): self.List = [] self.Types = dataTypes if archive: self.Archiver = Archiver(True) def FetchData(self, PackageName, Filename=None): try: import my_module except ImportError: return False if Filename != None: try: file_handle = open(Filename, 'a') except IOError: sys.exit(1) (amnt, header, self.List) = my_module.get_data(PackageName) This is the only way this code will work. As per my understanding, the bad part is that on every call of the method FetchData(), an import would be done. To not let that happen, I can put the import into __init__(). But when I put in there, I get a NameError saying that my_module is not available even though it got imported. All I noticed is that the import has to be part of the method else I end up getting a NameError. But always importing my_module is also not good. What is the correct way of doing this ? IMO, ideally it should be part of __init__() and be imported only when the class is instantiated. Thanks, Ritesh -- If possible, Please CC me when replying. I'm not subscribed to the list. The reason you can't put the import into the __init__ is that that is also a method, so the imported module is only available to that method's namespace. This is also true if you had put the import into any other method. The only way to make it global is to put your class into its own module and put the import before the class creation. Depending on how your instantiating the class, you may not need to worry about the whole importing thing. If you have all your code like this: class something(obj): def __init__(self): #do something def grabData(self): # do something import module x = something() y = something() Then the module gets imported only once. See http://www.python.org/search/hypermail/python-1993/0342.html I'm not sure what happens when you have one module calling another module that import a third module though. Mike -- http://mail.python.org/mailman/listinfo/python-list
Re: Namespace issue
On May 23, 12:20 pm, Ritesh Raj Sarraf [EMAIL PROTECTED] wrote: As per my understanding, the bad part is that on every call of the method FetchData(), an import would be done. To not let that happen, I can put the import into __init__(). But when I put in there, I get a NameError saying that my_module is not available even though it got imported. All I noticed is that the import has to be part of the method else I end up getting a NameError. But always importing my_module is also not good. How about something like this: class Dog(object): myimport = None def __init__(self): if not Dog.myimport: print importing... import os Dog.myimport = os def test(self): print Dog.myimport.listdir(./) d = Dog() d.test() print print d2 = Dog() d2.test() -- http://mail.python.org/mailman/listinfo/python-list
Re: Namespace issue
On May 23, 12:20 pm, Ritesh Raj Sarraf [EMAIL PROTECTED] wrote: As per my understanding, the bad part is that on every call of the method FetchData(), an import would be done. To not let that happen, I can put the import into __init__(). But when I put in there, I get a NameError saying that my_module is not available even though it got imported. All I noticed is that the import has to be part of the method else I end up getting a NameError. But always importing my_module is also not good. How about something like this: class Dog(object): myimport = None def __init__(self): if not Dog.myimport: print importing... import os Dog.myimport = os def test(self): print Dog.myimport.listdir(./) d = Dog() d.test() print print d2 = Dog() d2.test() --output:--- importing... [a bunch of files] [a bunch of files] -- http://mail.python.org/mailman/listinfo/python-list
Re: Namespace issue
On May 23, 12:20 pm, Ritesh Raj Sarraf [EMAIL PROTECTED] wrote: As per my understanding, the bad part is that on every call of the method FetchData(), an import would be done. To not let that happen, I can put the import into __init__(). But when I put in there, I get a NameError saying that my_module is not available even though it got imported. All I noticed is that the import has to be part of the method else I end up getting a NameError. But always importing my_module is also not good. How about something like this: class Dog(object): myimport = None def __init__(self): if not Dog.myimport: print importing... import os Dog.myimport = os def test(self): print Dog.myimport.listdir(./) d = Dog() d.test() print print d2 = Dog() d2.test() --output:--- importing... [a bunch of files] [a bunch of files] -- http://mail.python.org/mailman/listinfo/python-list
Re: namespace issue
def _gn(x): return x.upper() great_name = _gn class myclass: def mymethod(self, great_name=False): if great_name: return _gn('something') else: return 'something' def great_name(x): ... return x.upper() ... class myclass(object): ... def mymethod(self, great_name=False): ... if great_name: ... return globals()['great_name']('something') ... else: ... return 'something' ... myclass().mymethod() 'something' myclass().mymethod(True) 'SOMETHING' Thanks a lot for both suggestions, they were the things I was looking for. -- http://mail.python.org/mailman/listinfo/python-list
Re: namespace issue
Ooops, there was a typo in my previous mail: in the hope of the del statement only removing the local variable util ^ the above line should be: in the hope of the del statement only removing the local variable great_name -- http://mail.python.org/mailman/listinfo/python-list
Re: namespace issue
Daniel Nogradi wrote: I would like to give the same name to a keyword argument of a class method as the name of a function, with the function and the class living in the same namespace and the class method using the aforementioned function. So far I've been unsuccesfully trying to go along these lines: def great_name( x ): return x.upper( ) class myclass: def mymethod( self, great_name=False ): if great_name: return great_name( 'something' ) else: return 'something' def great_name(x): ... return x.upper() ... class myclass(object): ... def mymethod(self, great_name=False): ... if great_name: ... return globals()['great_name']('something') ... else: ... return 'something' ... myclass().mymethod() 'something' myclass().mymethod(True) 'SOMETHING' STeVe -- http://mail.python.org/mailman/listinfo/python-list
Re: namespace issue
On Thu, 13 Apr 2006 22:59:52 +0200, Daniel Nogradi wrote: I would like to give the same name to a keyword argument of a class method as the name of a function, with the function and the class living in the same namespace and the class method using the aforementioned function. That's a problem right there. As soon as you find yourself needing to distinguish between great_name the function and great_name the argument, you have a potential source of API confusion, no matter how great the name is. But if you absolutely must: def _gn(x): return x.upper() great_name = _gn class myclass: def mymethod(self, great_name=False): if great_name: return _gn('something') else: return 'something' -- Steven. -- http://mail.python.org/mailman/listinfo/python-list