Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

데이터분석 공부하기

[Function] 내장함수(builtin), 외장함수(extenal) 본문

Python

[Function] 내장함수(builtin), 외장함수(extenal)

Eileen's 2021. 8. 20. 19:18

[Function & Using reference]

-Builtin Function

-Python내 내장함수는 총 69개이다. (https://docs.python.org/3/library/functions.html)-각 함수를 잘 알면 좋을 듯-dir()을 써서 어떤 builtin function이 있는지 알아보기 쉽지 않다.*알아보는 법: python API documentation : https://www.python.org

 

-External Function(어느 함수인지 잘 알아보기)

*알아보는 법:

1) dir()로 np, pd를 넣어 그 안에 function list를 볼 수 있다.

*반드시 그 함수를 Call해야지만 dir()/help()를 사용할 수 있다. sklearn은 subpackages를 자동으로 불러오지 않기때문에,

sklearn.tree에 대해 궁금하면 sklearn이 아닌 sklearn.tree를 import한 후 dir(),help()을 사용해야한다.

   -> pandas as pd 등 rename을 한 경우 반드시 pd로 알아보아야 한다.

2) 여기 있으면, help()으로 더 자세히 알아볼 수 있다. 

  -> 이때, help(mean)이 아니라, libraries도 모두 포함해야한다. help(np.mean)/help(pd.DataFrame.mean <-DataFrame 대소문자 주의)과 같이.

  -> 함수 뒤 괄호()는 빼야 한다.

 

*알아보는 법2: 특히, pd에서 어떤 module안에 mean이 있는 지 헷갈린다면 아래 doc에서 mean을 쳐보면 된다.

pandas API documentation : https://pandas.pydata.org/docs/index.html

numpy API documentation : pandas/ numpy API documentation 

 

-Q & A :

1) python에 mean 내장함수는 없다. 대신 statistics를 import하거나, np.mean or pd.DataFame.mean 등을 사용하면 된다.

2) python built-in함수를 코딩창에서 찾아볼 수는 없나?

    1. dir(__builtins__)  help(__builtins__)

    2. type(open) >>> builtin_function_or_method

    3.  import types
         isinstance(pow, types.BuiltinFunctionType) 

    4. import inspect
        inspect.isbuiltin(pow) True 

3) list에 소속된 함수는 무엇인가, 코딩창에서 찾아볼 수 없나? dir(list)/ help(list)하면 가능하다.

4) pandas/numpy는 libraries이다, DataFrame과 List는 뭘까? List is a type of data and DataFrame is a structure of data.

-Question: Why do some methods use dot notation when others don’t?

-Answer: Python is an object-oriented language, meaning it’s built around the idea of objects making things more scalable and reusable. You’ll be introduced to objects in depth later in the course, but for now think of objects as being built from blueprints that specify that object’s properties and methods.
Dot notation allows us to use methods belonging to an object. For example, if we had a Car object it might have an ignition( ) method that we can use to turn the car on. Strings are objects just like a Car could be, and they have methods you can make use of, like upper( ) and lower( ). Other methods that don’t use dot notation are not dependent on being attached to an object, and might even be built into the language, like the len( ) method you learned about!

 

[연습]

dtypes은 builtin일까 external일까? external이면 어느 library/object의 소속으로 어떻게 써야할까?

1) type(dtypes)로 builtin인지 확인 -> 에러임으로 아니다.

2) 몇가지 libraries 체크: dir(pd.DataFrame) -> dtypes를 찾았다. pd.DataFrame.dtypes다.

 

 

 

-함수(function): 특정 기능을 한다(매개변수를 이용해 자료 전달)

def 함수이름(매개변수):

     <수행할 명령>

     return 반환값

 

ex>

def solve(a, b): #a,b는 매개변수
    return a * b
    
var1 = solve(3, 4) #3,4,['Cham']은 인자
var2 = solve(3, ['Cham'])


 

*매개변수(parameter): 함수안에서 사용되는 변수; 함수를 정의할때 (만들때) 넘겨받은 값을 관리하는 변수

*인자(argument): 함수를 호출할때(사용할 때) 함수로 넘겨주는 자료

 

*반환(Return): 함수 외부로 값을 전달 (함수 내부에서 일어난 일은 함수 외부에서 알 수 없다.)

 

 

 

-Mehtod: 특정 자료와 연관 지어 특정 기능을 한다(자료 뒤에 .을 찍어 사용)

ex> list.sort(), list.clear()