By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,330 Members | 1,135 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,330 IT Pros & Developers. It's quick & easy.

access the name of my method inside it

P: n/a
Hi,

for example I have this method

def my_method():
# do something

# how do I get the name of this method which is my_method here?

Thanks,
james

Aug 1 '07 #1
Share this Question
Share on Google+
12 Replies

P: n/a
On Wed, 01 Aug 2007 09:06:42 +0000, james_027 wrote:
for example I have this method

def my_method():
# do something

# how do I get the name of this method which is my_method here?
Why do you need this? There are ways but those are not really good for
production code.

Ciao,
Marc 'BlackJack' Rintsch
Aug 1 '07 #2

P: n/a
Marc 'BlackJack' Rintsch wrote:
On Wed, 01 Aug 2007 09:06:42 +0000, james_027 wrote:
>for example I have this method

def my_method():
# do something

# how do I get the name of this method which is my_method here?

Why do you need this? There are ways but those are not really good for
production code.
Maybe he wants to write a recursive method?

Once way is to call self.__calss__.mymethod(self). Ugly, isn't it?
>>class p:
.... def mymethod(self, n):
.... if n <= 1:
.... return 1
.... else:
.... return n * self.__class__.mymethod(self, n-1)
....
>>pp = p()
pp.mymethod(10)
3628800
>>>
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Aug 1 '07 #3

P: n/a
On Wed, 01 Aug 2007 07:01:42 -0400, Steve Holden wrote:
Marc 'BlackJack' Rintsch wrote:
>On Wed, 01 Aug 2007 09:06:42 +0000, james_027 wrote:
>>for example I have this method

def my_method():
# do something

# how do I get the name of this method which is my_method here?

Why do you need this? There are ways but those are not really good for
production code.
Maybe he wants to write a recursive method?

Once way is to call self.__calss__.mymethod(self). Ugly, isn't it?
Ugly yes, unnecessary convoluted yes, solution no. You typed `my_method`
in the source. The OP wants to know how to avoid that.
>>class p:
... def mymethod(self, n):
... if n <= 1:
... return 1
... else:
... return n * self.__class__.mymethod(self, n-1)
Why not simply ``self.mymethod(n - 1)`` instead!?

Ciao,
Marc 'BlackJack' Rintsch
Aug 1 '07 #4

P: n/a
Marc 'BlackJack' Rintsch wrote:
On Wed, 01 Aug 2007 07:01:42 -0400, Steve Holden wrote:
>Marc 'BlackJack' Rintsch wrote:
>>On Wed, 01 Aug 2007 09:06:42 +0000, james_027 wrote:

for example I have this method

def my_method():
# do something

# how do I get the name of this method which is my_method here?
Why do you need this? There are ways but those are not really good for
production code.
Maybe he wants to write a recursive method?

Once way is to call self.__calss__.mymethod(self). Ugly, isn't it?

Ugly yes, unnecessary convoluted yes, solution no. You typed `my_method`
in the source. The OP wants to know how to avoid that.
> >>class p:
... def mymethod(self, n):
... if n <= 1:
... return 1
... else:
... return n * self.__class__.mymethod(self, n-1)

Why not simply ``self.mymethod(n - 1)`` instead!?
Well, absolutely no reason if you want to go making things simple ;-)

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Aug 1 '07 #5

P: n/a
Hi,

On Aug 1, 5:18 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Wed, 01 Aug 2007 09:06:42 +0000, james_027 wrote:
for example I have this method
def my_method():
# do something
# how do I get the name of this method which is my_method here?

Why do you need this? There are ways but those are not really good for
production code.
I am going to use this in Django. I am trying to implement a
permission here, where in the database store the methods that the user
are allowed to execute. for example if the method is def
create_event(): the method will look for create_event in the database
to see if it allow to be execute.

Thanks.
james

Aug 1 '07 #6

P: n/a
On Aug 1, 8:07 am, james_027 <cai.hai...@gmail.comwrote:
Hi,

On Aug 1, 5:18 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Wed, 01 Aug 2007 09:06:42 +0000, james_027 wrote:
for example I have this method
def my_method():
# do something
# how do I get the name of this method which is my_method here?
Why do you need this? There are ways but those are not really good for
production code.

I am going to use this in Django. I am trying to implement a
permission here, where in the database store the methods that the user
are allowed to execute. for example if the method is def
create_event(): the method will look for create_event in the database
to see if it allow to be execute.

Thanks.
james
How about using a decorator? Here is a rough version:

def checkPrivs(fn):
fnName = fn.func_name
def restricted(*args):
print "about to call function", fnName
if fnName in listOfAllowedFunctions:
return fn(*args)
else:
raise KeyError("you don't have sufficient privileges to do
THAT")
return restricted

listOfAllowedFunctions = ['add','subtract']

@checkPrivs
def add(a,b):
return a+b

@checkPrivs
def subtract(a,b):
return a-b

@checkPrivs
def multiply(a,b):
return a*b

add(1,2)
subtract(4,1)
multiply(3,2)

-- Paul
Aug 1 '07 #7

P: n/a
james_027 wrote:
Hi,

On Aug 1, 5:18 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
>On Wed, 01 Aug 2007 09:06:42 +0000, james_027 wrote:
>>for example I have this method
def my_method():
# do something
# how do I get the name of this method which is my_method here?
Why do you need this? There are ways but those are not really good for
production code.

I am going to use this in Django. I am trying to implement a
permission here, where in the database store the methods that the user
are allowed to execute. for example if the method is def
create_event(): the method will look for create_event in the database
to see if it allow to be execute.
This might help, I used it for a similar need I had in the past where I needed a method to know the name it was called with

class TestClass:
def __init__(self):
pass

def __getattr__(self, name):
try:
return getattr(self.__class__, name)
except AttributeError:
return functools.partial(self.foo, name)

def foo(self, name, **args):
print name
for i in args:
print " %s=%s" % (i, args[i])
Aug 1 '07 #8

P: n/a
james_027 a écrit :
Hi,

On Aug 1, 5:18 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
>On Wed, 01 Aug 2007 09:06:42 +0000, james_027 wrote:
>>for example I have this method
def my_method():
# do something
# how do I get the name of this method which is my_method here?
Why do you need this? There are ways but those are not really good for
production code.

I am going to use this in Django. I am trying to implement a
permission here, where in the database store the methods that the user
are allowed to execute. for example if the method is def
create_event(): the method will look for create_event in the database
to see if it allow to be execute.
Then the solution is definitively to use a decorator, cf Paul's answer.
Aug 1 '07 #9

P: n/a
def my_method():

# do something
# how do I get the name of this method
# which is my_method here?

Why do you need this? There are ways but those
are not really good for production code.

I am going to use this in Django. I am trying to implement
a permission here, where in the database store the methods
that the user are allowed to execute.

for example if the method is def create_event() :
the method will look for create_event in the database
to see if it allow to be execute.
james ....

Perhaps a simple-minded hard-coded solution
might work ....

def permission_check( name ) :

permission = DB.permission_check( name )

if not permission :

print 'No Execute Permission for %s ' % name

sys.exit( -1 )

def my_method() :

permission_check( "my_method" )

....
--
Stanley C. Kitching
Human Being
Phoenix, Arizona
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Aug 1 '07 #10

P: n/a
Cousin Stanley a écrit :
>>>>def my_method():

# do something

# how do I get the name of this method
# which is my_method here?

Why do you need this? There are ways but those
are not really good for production code.

I am going to use this in Django. I am trying to implement
a permission here, where in the database store the methods
that the user are allowed to execute.

for example if the method is def create_event() :
the method will look for create_event in the database
to see if it allow to be execute.


james ....

Perhaps a simple-minded hard-coded solution
might work ....

def permission_check( name ) :

permission = DB.permission_check( name )

if not permission :

print 'No Execute Permission for %s ' % name

sys.exit( -1 )
An exception would be better here.
def my_method() :

permission_check( "my_method" )

....
May I suggest ?

class Unauthorized(Exception): pass

def check_permission(permission):
if not DB.check_permission(permission):
raise Unauthorized(permission)

def requires_perm(func):
def with_check(*args, **kw):
check_permission(func.__name__)
return func(*args, **kw)
return with_check

@requires_perm
def my_method():
# do something useful here
Aug 1 '07 #11

P: n/a
Hi all!

Thanks for all your idea, this community is truly a great one!

james

Aug 2 '07 #12

P: n/a
On 8/1/07, james_027 <ca********@gmail.comwrote:
Hi,

On Aug 1, 5:18 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Wed, 01 Aug 2007 09:06:42 +0000, james_027 wrote:
for example I have this method
def my_method():
# do something
# how do I get the name of this method which is my_method here?
Why do you need this? There are ways but those are not really good for
production code.

I am going to use this in Django. I am trying to implement a
permission here, where in the database store the methods that the user
are allowed to execute. for example if the method is def
create_event(): the method will look for create_event in the database
to see if it allow to be execute.

Thanks.
james
I would suggest instead setting an attribute on the function object
via a decorator, and then have your mod_python handler check for the
presence of that attribute on the function that is being called,
rather than doing a DB lookup.

--
Evan Klitzke <ev**@yelp.com>
Aug 2 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.