namespace issue

2006-04-13 Thread Daniel Nogradi
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'

This would fail, because in the namespace of mymethod great_name is a
local variable and is not a callable. So I tried to modify the class
like this:

class myclass:
def mymethod( self, great_name=False ):
great_name_ = great_name
del great_name
if great_name_:
return great_name( 'something' )
else:
return 'something'

in the hope of the del statement only removing the local variable util
but still remembering the great_name function from outside, but this
didn't work either. So my question is if it was possible to do this at
all?

The reason for giving the same name is a usability issue of my module,
I would like both the keyword argument and the function to be visible
by the user and the name I would like to give them describes very well
what they are doing. That is also the reason why I don't want to hide
the great_name function in the class as a method.
-- 
http://mail.python.org/mailman/listinfo/python-list


Namespace issue

2007-05-23 Thread Ritesh Raj Sarraf
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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: namespace issue

2006-04-13 Thread Daniel Nogradi
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

2006-04-13 Thread Steven Bethard
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

2006-04-13 Thread Steven D'Aprano
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


Re: namespace issue

2006-04-14 Thread Daniel Nogradi
> 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

2007-05-23 Thread kyosohma
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

2007-05-23 Thread kyosohma
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

2007-05-23 Thread 7stud
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

2007-05-23 Thread 7stud
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

2007-05-23 Thread 7stud
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