[Python] Difference between function and method

[Note] Differences between python functions and methods

I thought that an object followed by a "." And a "()" was a method, but range () is called a function.

As a result of investigation, methods and functions were used properly, so a summary.

Difference between function and method

① Method

Only available in ** specific classes **.

Example: replace method It cannot be used for list (array), but it can be used for str (character string).

replace cannot be used in list


list = ['AAA', 'BBB', 'CCC']
list.replace("A","B")

#output
# AttributeError: 'list' object has no attribute 'replace'

can be used with str


list = ['AAA', 'BBB', 'CCC']
str(list).replace("A","B")

#output
# "['BBB', 'BBB', 'CCC']"

### ② Function Can be used for ** a wide range of objects ** (not tied to a particular class).

The ** built-in function ** that is built into python by default is applicable.

Main built-in functions

function Contents
type() Returns the type
tuple() Convert to tuple type
str() Convert to a string
set() Convert to set type
range() Returns an integer included in the specified range
open() Open file
list() Convert to list type
len() Returns the number of elements
int() Returns an integer (rounded down to the nearest whole number)
format() Change the format

-A list of built-in functions is here

Example: str function

Can be used for tuple


A = 1,2,3,4,5
type(A)  #Output: tuple

type(str(A))

#output
# str

Can be used for list


B = [1,2,3,4,5]
type(B)  #Output: list

type(str(B))

#output
# str

Can be used for set


C = {1,2,1,5,2,3,4,5}
type(C)  #Output: set

type(str(C))

#output
# str

Recommended Posts

[Python] Difference between function and method
Difference between Ruby and Python split
Difference between java and python (memo)
Difference between list () and [] in Python
Difference between python2 series and python3 series dict.keys ()
Python --Difference between exec and eval
[Python] Difference between randrange () and randint ()
[Python] Difference between sorted and sorted (Colaboratory)
List concatenation method in python, difference between list.extend () and “+” operator
difference between statements (statements) and expressions (expressions) in Python
Difference between @classmethod and @staticmethod in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
[Python Iroha] Difference between List and Tuple
[python] Difference between rand and randn output
File open function in Python3 (difference between open and codecs.open and speed comparison)
[python] Difference between variables and self. Variables in class
About the difference between "==" and "is" in python
Difference between process and job
Difference between "categorical_crossentropy" and "sparse_categorical_crossentropy"
Difference between regression and classification
Difference between np.array and np.arange
Difference between MicroPython and CPython
Difference between ps a and ps -a
Difference between return and print-Python
Difference between Ruby and Python in terms of variables
Difference between return, return None, and no return description in Python
Difference in writing method to read external source code between Ruby and Python
Difference between SQLAlchemy filter () and filter_by ()
python function ①
Python module num2words Difference in behavior between English and Russian
Python> Difference between inpbt and print (inpbt) output> [1. 2. 3.] / array ([1., 2., 3.], dtype = float32)
Memorandum (difference between csv.reader and csv.dictreader)
[Python] function
(Note) Difference between gateway and default gateway
Difference between Numpy randint and Random randint
Differences between Python, stftime and strptime
Difference between sort and sorted (memorial)
Function synthesis and application in Python
Difference between SQLAlchemy flush () and commit ()
python function ②
[Python] Function arguments * (star) and ** (double star)
Difference in how to write if statement between ruby ​​and python
Transcendental simple and clear! !! Difference between single quotes and double quotes in Python
Python / Numpy> Link> Difference between numpy.random and random.random> thread-safe or not
Communicate between Elixir and Python with gRPC
Differences in authenticity between Python and JavaScript
Differences between Ruby and Python in scope
[Xg boost] Difference between softmax and softprob
[Django ORM] Difference between values () and only ()
Test method for size difference between groups
Difference between linear regression, Ridge regression and Lasso regression
Difference between docker-compose env_file and .env file
[Python3] Switch between Shift_JIS, UTF-8 and ASCII
speed difference between wsgi, Bottle and Flask
Differences in multithreading between Python and Jython
Differences between Ruby and Python (basic syntax)
Difference between numpy.ndarray and list (dimension, size)
Correspondence between Python built-in functions and Rust
Difference between ls -l and cat command
Exchange encrypted data between Python and C #