Python Forum

Full Version: Is there any way to check if a function is user-defined?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Sometimes it would be nice to single out the functions defined by users in a big project. So is there any way to differentiate user-defined functions from library functions? Thanks.
Your going to have to be more specific. User defined functions in what?
(Oct-05-2016, 02:20 PM)metulburr Wrote: [ -> ]Your going to have to be more specific. User defined functions in what?

As long as a function is defined by a user no matter where the function is defined.
Usually it's pretty straight forward, if I understand your question correctly. If you see something like
def a_function():
it will be a user defined function

If you see something like
import math

variable = math.ceil(x)
It will be a function from a library, in this case 'math'
(Oct-05-2016, 03:28 PM)sparkz_alot Wrote: [ -> ]Usually it's pretty straight forward, if I understand your question correctly. If you see something like
def a_function():
it will be a user defined function

If you see something like
import math

variable = math.ceil(x)
It will be a function from a library, in this case 'math'

I thought he was talking about functions defined be each user  :doh:
(Oct-05-2016, 03:28 PM)sparkz_alot Wrote: [ -> ]Usually it's pretty straight forward, if I understand your question correctly. If you see something like
def a_function():
it will be a user defined function

If you see something like
import math

variable = math.ceil(x)
It will be a function from a library, in this case 'math'
Given a function a_function in your case, how would you check if a_function is a user-defined function programmatically?
You may be right :P

(Oct-05-2016, 03:32 PM)dullboy Wrote: [ -> ]
(Oct-05-2016, 03:28 PM)sparkz_alot Wrote: [ -> ]Usually it's pretty straight forward, if I understand your question correctly. If you see something like
def a_function():
it will be a user defined function

If you see something like
import math

variable = math.ceil(x)
It will be a function from a library, in this case 'math'
Given a function a_function in your case, how would you check if a_function is a user-defined function programmatically?

I suppose you could open the file and search it for lines beginning with 'def' then doing something with that information.
I think if there is some kind of software that manage the project there will be records what piece of code from who is written.

If a user write a function to a file this file should have an owner.
victor@jerry:~$ ls -l | head -n 5
total 2079052
-rw-rw-r-- 1 victor victor    132737 Mar  1  2016 \
-rw-rw-r-- 1 victor victor      1267 Sep  9 15:52 00349d0ac60ab0cab5e5-f19784922882fd6982c917852d90fff798155313.zip
-rw-rw-r-- 1 victor victor    398422 Sep 16 09:49 0.18.1
-rw-rw-r-- 1 victor victor      2494 Apr 26 21:02 10.1371%2Fjournal.pone.0056779.bib
As you can see victor victor is the owner of those 5 files or directories. First is the owner the second is the group.
If by user-defined functions you mean functions that you've written yourself in your current file, like:

def myfunc(): pass
Then having a print statement could be of some help:

import types
print [f.__name__ for f in globals().values() if type(f) == types.FunctionType]
That statement will print the name of all type 'function' objects.

The thing is, if you import a function directly from a module:

from requests import get
The get function from the requests module will now be listed in the previously mentioned print statement.

PS: in my print statement, it should read f.__name__
There's also the inspect module, if you really want to dive into the deep end...
Pages: 1 2