Python Forum
Is there any way to check if a function is user-defined?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is there any way to check if a function is user-defined?
#1
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.
Reply
#2
Your going to have to be more specific. User defined functions in what?
Recommended Tutorials:
Reply
#3
(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.
Reply
#4
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'
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
(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:
Recommended Tutorials:
Reply
#6
(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?
Reply
#7
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.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#8
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
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__
Reply
#10
There's also the inspect module, if you really want to dive into the deep end...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 576 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,281 Sep-03-2023, 03:22 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,074 Dec-25-2022, 03:00 PM
Last Post: askfriends
  Getting NameError for a function that is defined JonWayn 2 1,092 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,874 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  How to print the output of a defined function bshoushtarian 4 1,279 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  User-defined function to reset variables? Mark17 3 1,644 May-25-2022, 07:22 PM
Last Post: Gribouillis
  Multiple user defined plots with secondary axes using for loop maltp 1 1,442 Apr-30-2022, 10:19 AM
Last Post: maltp
  Date format and past date check function Turtle 5 4,233 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  Exit function from nested function based on user input Turtle 5 2,896 Oct-10-2021, 12:55 AM
Last Post: Turtle

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020