473,387 Members | 1,702 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,387 software developers and data experts.

access the name of my method inside it

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
12 1137
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
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
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
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
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
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
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
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
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
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
Hi all!

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

james

Aug 2 '07 #12
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: jason | last post by:
Hi Ray...a while ago you explained an elegant solution to enable me to CREATE and EDIT existing tables and queries inside my online access 2000 database.... could you provide refresher links on...
1
by: bill yeager | last post by:
I did some more debugging and found the following: 1) I placed the following code in the button event just to see if I could cycle thru the datagrid control collection: <code> Dim strhello As...
1
by: rodrigo | last post by:
This is what I am doing public class GridCustomPaging : System.Web.UI.Page { protected System.Web.UI.WebControls.DataGrid DgSearch;
10
by: Davíð Þórisson | last post by:
Please can someone tell me how on earth to create an instance of my top level (base) Page class so that I can access it's objects from an user control?? Someone told me public myParent =...
4
by: James | last post by:
I have a VB windows forms application that accesses a Microsoft Access database that has been secured using user-level security. The application is being deployed using No-Touch deployment. The...
11
by: glen.coates.bigworld | last post by:
I'm developing a library at the moment that involves many classes, some of which have "exposed" capabilities. I'm trying to design a nice interface for both exposing those capabilities, and...
5
by: Anil Gupte | last post by:
How does one access dynamic controls by name (or whatever other means)? I have the following: Dim newbtnPick As New Button newbtnPick.Name = "SliceButton" & CurSliceNum newbtnPick.Location =...
9
by: JT | last post by:
Here is the overall structure I will be referring to: End-program ProvideWorkFlow.dll Forms and methods that properly manipulate calls to methods in AccessUtils AccessUtils (a web service)...
7
by: Andy B | last post by:
I have a class I am creating for data access. I need to access controls from inside the class that are on a particular page. How do I do this? or is creating an instance of the page class and using...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.