Python Forum
Why built in functions are defined as class?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why built in functions are defined as class?
#1
In python official documentation there is a list of built in functions. But if I click on some of them, instead of taking me to a function definition, it takes me to a class definition. But if they are classes, then why they are under function definition?

For example, if I click on slice() function, it takes me to class slice
Reply
#2
Quote:Built-in Functions
The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order.

The title is missleading. Some are not functions but types. Only that they are lowercase.
To see if a builtin is a class or a function that just type the name of it without parenthesis.
quazirfan likes this post
Reply
#3
look at it this way
abs is a class of type builtin_function_or_method, abs(14) is an instance of class abs with attribute 14
>>> type(abs)
<class 'builtin_function_or_method'>

>>>type(abs(14)) # returns type of instance of abs (which is of class int)
<class 'int'>
Reply
#4
(Oct-05-2021, 02:42 AM)Larz60+ Wrote: look at it this way
abs is a class of type builtin_function_or_method, abs(14) is an instance of class abs with attribute 14
>>> type(abs)
<class 'builtin_function_or_method'>

>>>type(abs(14)) # returns type of instance of abs (which is of class int)
<class 'int'>

No, no! That's totally wrong!! abs is not a class. The type of abs is a class.

Anyhow, to see if a builtin is a class or not, you dont need to use type. Just write the name of the builtin and press enter.

>>> bytearray
<class 'bytearray'>
so bytearray is a class

>>> abs
<built-in function abs>
so abs is a builtin function

Wall Wall Wall Wall

Big Grin Big Grin Big Grin Big Grin
quazirfan likes this post
Reply
#5
(Oct-05-2021, 02:42 AM)Larz60+ Wrote: >>>type(abs(14)) # returns type of instance of abs (which is of class int)

From abs() documentation, abs(x) Return the absolute value of a number. I think it is not correct to say instance of abs as abs function returns a number, or int in this case.
ndc85430 likes this post
Reply
#6
They should be called built-in callables instead of built-in functions.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  "Name is not defined" when running a class lil_e 6 3,769 Jan-12-2023, 11:57 PM
Last Post: lil_e
  Calling functions from within a class: PYQT6 Anon_Brown 4 3,643 Dec-09-2021, 12:40 PM
Last Post: deanhystad
  Error when refering to class defined in 'main' in an imported module HeRo 2 2,334 Apr-13-2021, 07:22 PM
Last Post: HeRo
  Question about functions(built-in) Jinja2Exploitation 2 1,897 Nov-15-2020, 02:13 PM
Last Post: jefsummers
  should I ... go class or stick with functions? 3Pinter 4 2,032 Nov-14-2020, 10:40 AM
Last Post: 3Pinter
  Running scripts and location of saved interpreted user-defined classes and functions leodavinci1990 3 2,468 Aug-25-2020, 03:43 AM
Last Post: micseydel
  "Class already defined" while using typings. DreamingInsanity 0 2,304 Aug-19-2020, 10:43 AM
Last Post: DreamingInsanity
  python library not defined in user defined function johnEmScott 2 3,774 May-30-2020, 04:14 AM
Last Post: DT2000
  User defined functions inside other user defined functions WildP 1 1,916 Jan-29-2020, 04:57 PM
Last Post: Clunk_Head
  using class functions in roguelike game trousers1 3 2,521 Dec-02-2019, 08:22 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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