473,398 Members | 2,380 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,398 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 1138
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.