473,382 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

namespace issue

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.
Apr 13 '06 #1
3 1025
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
Apr 14 '06 #2
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.

Apr 14 '06 #3
> 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.
Apr 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jon | last post by:
Hello all, Basic question. I have a vector/matrix class I'm building, and I seem to be getting errors such as this: ..\otherFileUsingVectorMatrixClass.cpp(182) : error C2064: term does not...
3
by: Jason Heyes | last post by:
There are 3 files in the project. I compiled it with VC++ 6.0 and got an unresolved external symbol error. What am I doing wrong? // Foo.cpp #include "Foo.h" using namespace foo; void f() {...
8
by: Jason Heyes | last post by:
I wrote: namespace { void f(); } void f() { std::cout << "hello world" << std::endl; } When I compile, I receive this error: unresolved external symbol "bool __cdecl `anonymous...
4
by: Richard L Rosenheim | last post by:
Here's a sample XML file (created by saving a dataset): <?xml version "1.0" standalone="yes"?> <dsData xmlns="http://tempuri.org/DataSchema.xsd"> <Configuration> </Configuration> <Data>...
6
by: Robert Warnestam | last post by:
I've two class libraries, where one is referencing the other. The first library looks like this; (assembly name and root name space is set to Codab.Parser) namespace Codab.Parser { public class...
0
by: Charles Leonard | last post by:
I need opinions regarding Web Service Namespace usage and/or suggestions regarding how to handle deployment. Our web service application will exist in two different domains and is likely to...
2
by: Pathogenix | last post by:
Greetings, I'm trying to fix a web service client which has been implemented in a dailywtf worthy manner. I've decided to rip all the old code out and start again from the proxy generated by...
0
by: mazdotnet | last post by:
Hi everyone, I have the following very odd problem (only happens on Windows 2003 Server and not XP) In my class I have (no namespace) class test { ... public static void method...
4
by: pc.candy | last post by:
Hi there I'm having some difficulty with transforming xml data with more than one namespace defined in the xml. I'm able to bring back some values of the xml but not others in another...
0
by: thomson | last post by:
Hi All, i have a folder in my project BO, and i added the core classes from an existing 2005 application and added in this folder, and i added a namespace for it, but in fact when i compiled and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.