Python Forum
Is there any way to check if a function is user-defined? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Is there any way to check if a function is user-defined? (/thread-311.html)

Pages: 1 2


Is there any way to check if a function is user-defined? - dullboy - Oct-05-2016

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.


RE: Is there any way to check if a function is user-defined? - metulburr - Oct-05-2016

Your going to have to be more specific. User defined functions in what?


RE: Is there any way to check if a function is user-defined? - dullboy - Oct-05-2016

(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.


RE: Is there any way to check if a function is user-defined? - sparkz_alot - Oct-05-2016

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'


RE: Is there any way to check if a function is user-defined? - metulburr - Oct-05-2016

(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:


RE: Is there any way to check if a function is user-defined? - dullboy - Oct-05-2016

(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?


RE: Is there any way to check if a function is user-defined? - sparkz_alot - Oct-05-2016

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.


RE: Is there any way to check if a function is user-defined? - wavic - Oct-05-2016

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.


RE: Is there any way to check if a function is user-defined? - Crimson King - Oct-05-2016

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__


RE: Is there any way to check if a function is user-defined? - nilamo - Oct-05-2016

There's also the inspect module, if you really want to dive into the deep end...