namespace issue

D

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.
 
S

Steven Bethard

Daniel said:
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'
.... return x.upper()
........ def mymethod(self, great_name=False):
.... if great_name:
.... return globals()['great_name']('something')
.... else:
.... return 'something'
....'SOMETHING'


STeVe
 
S

Steven D'Aprano

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'
 
D

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'
... return x.upper()
...... def mymethod(self, great_name=False):
... if great_name:
... return globals()['great_name']('something')
... else:
... return 'something'
...'SOMETHING'



Thanks a lot for both suggestions, they were the things I was looking for.
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top