거대한 정보의 더미들/파이썬 코딩테스트 정리

파이썬의 모듈과 라이브러리의 차이 + Compiler Flag + Identifier(식별자)

PurpleGuy101 2023. 1. 22. 14:38

Chatgpt에게 물어보았다 

 

(질문 : 파이썬에서 모듈을 import 하는 것이 C언어에서 라이브러리를 사용하는 것과 같은것일까요?)

 

답변 :

 

Importing a module in Python and using a library in C are similar in that they both allow you to use pre-existing code in your own program. However, there are some differences in how they are used and implemented.

 

파이썬 상에서 모듈을 사용하는 것과 C에서 라이브러리를 사용하는 것은 이미 만들어진 코드를 사용할 수 있게 해준다는 점에서 비슷하다고 할 수 있죠. 하지만 그들이 어떻게 쓰이고 구현되는지는 몇몇 차이점이 존재합니다.

 

In Python, a module is a file containing Python definitions and statements. To use the code in a module, you use the import statement followed by the name of the module. Once the module is imported, you can access the functions, classes, and variables defined in the module using the dot notation (e.g. module_name.function_name()).

For example:

 

파이썬에서는, 모듈은 파이썬의 정의(definition)과 명령문(statement)를 담는 파일입니다. 모듈에 있는 코드를 사용하기 위해서는, 모듈의 이름을 사용한 import 명령문을 사용하면 되죠. 이러한 모듈이 imported되었다면, 당신은 모듈에서 정의된 함수, 클래스, 변수들을 dot noation(점표기법, ex . 모듈의이름.함수의이름() 처럼)을 통해 사용할 수 있습니다.

 

import math x = math.sin(math.pi / 2) print(x)

 

 

In C, a library is a collection of object files that can be linked with a program at compile time. To use the code in a library, you include the appropriate header file in your program using the #include directive and link your program with the library using the appropriate compiler flag. Once the library is included, you can access the functions, variables, and data structures defined in the library by prefixing the appropriate identifier with the library name (e.g. library_name_function())

For example:

 

C에서 라이브러리는 컴파일과정에서 링킹될 수 있는 오브젝트 파일들의 모임입니다. 라이브러리의 코드를 사용하고 싶다면, 사용자는 #include라는 directive(선행처리 지시어)를 통해 헤더파일을 사용하거나 프로그램이 적절한 컴파일러 플래그 (*1:compiler flag, GCC컴파일러에서 뒤에 붙이는 -o같은 것들)를 사용하여 라이브러리를 사용할 수 있습니다. 라이브러리가 included되고 나면 당신은 라이브러리에서 library_name_function()과 같이 적절한 indentifier(*2:식별자)를 붙여서 정의된  함수, 변수, 자료형을 사용할 수 있습니다.

Copy code
#include <math.h> double x = sin(M_PI / 2); printf("%lf", x);

So, in short, importing a module in Python is similar to including a header file in C, and using functions, variables, and data structures from the module or library is similar to calling functions, variables, and data structures from the library in C.

 

그래서 요약하자면, 파이썬에서 모듈을 importing하는 것은 C에서 헤더파일을 include 하는 것과 비슷하며

파이썬에서 함수, 변수와 자료형을 모듈이나 라이브러리에서 가져오는 것은 

C에서 함수, 변수, 자료형을 라이브러리에서 가져오는 것과 비슷합니다.

 

############################################################

 

-> 내가 잘못 생각하던 부분은,  모듈과 라이브러리가 같은 레벨에서 대응되는 개념이 아니라는 점이다.

 

파이썬의 모듈은 오히려 C언어의 헤더파일에 대응되는 내용이며

라이브러리란 모듈들의 모음집을 의미한다. 다시한번 챗선생에게 물어보자.

 

A module in Python is a single file that contains Python code, while a library is a collection of modules that provide additional functionality for a Python program. A module can be imported into another Python program and used to perform specific tasks, while a library is a collection of modules that are designed to work together to perform a specific set of related tasks. Additionally, libraries in Python are usually distributed and installed separately from the main Python interpreter, while modules are typically included with the Python interpreter distribution.

 

파이썬의 모듈은 파이썬으로 작성된 하나의 코드가 담긴 파일일뿐입니다.

그에 반해 라이브러리는 모듈의 모음집이며 파이썬 프로그램에 추가적인 기능을 제공해주죠.

 

모듈은 다른 파이썬 프로그램에 import되어 특정 작업을 수행할 수 있는데 반해서,

라이브러리는 관련된 특정 작업을 수행하도록 여러가지 관련된 모듈들을 모아놓은 모음집입니다.

(* 아두이노 중급자 키트가 라이브러리라면, 안에 있는 소자, 전선, 서보모터들은 모듈인셈)

추가적으로, 파이썬의 라이브러리들은 저장공간의 여기저기에 흩어져있고

메인 파이썬 인터프리터와 따로 설치되며 (*+아마 자체업데이트로 버젼관리가 힘들고)

모듈은 파이썬 인터프리터에 저장공간에 같이 저장됩니다.  

 

#############################################################

추가설명

#######

 

(*1:Complier Flag,컴파일러 플래그) 

A compiler flag is a command line option that is passed to the compiler when building your program. These flags can be used to control various aspects of the compilation process, such as enabling or disabling certain features, specifying the optimization level, or setting the target platform.

컴파일러 플래그는 당신이 프로그램을 만들때, 컴파일러에게 그대로 전달되는 명령문입니다. 이러한 플래그를 통해 컴파일과정의 여러요소를 제어할 수 있습니다. 예를 들자면 어떠한 특징을 사용하거나 안하게 하거나, 컴파일의 최적화 레벨을 설정하거나, 아니면 프로그램을 수행할 환경이 되는 목표 플랫폼을 설정할 수 있습니다.

 

In the context of linking with a library, a compiler flag is used to specify the location of the library files and the library name. This allows the compiler to link the library's object files with the object files of your program, so that the functions and variables defined in the library can be used in your program.

라이브러리를 링킹하는 과정에서, 컴파일러 플래그는 라이브러리의 파일위치와 이름을 알려줍니다. 이것은 컴파일러로 하여금 라이브러리의 오브젝트파일들을 당신의 프로그램에 링킹해주고, 그러므로써 라이브러리의 함수나 변수를 프로그램이 사용할 수 있도록 해줍니다.

 

For example, in GCC (GNU Compiler Collection), the flag -l is used to link with a library and the flag -L is used to specify the directory where the library files are located.

For example:

예를 들자면, GCC 컴파일러에서는 -l이라는 플래그는 라이브러리와 링킹하는데 쓰이고, -L 플래그는 라이브러리 파일가 위치한 디렉토리 경로를 지정하는데 사용됩니다.

Copy code
gcc -o myprogram myprogram.c -lm -L/usr/local/lib

This command will compile the myprogram.c file, link the math library (-lm) and specify that the library files are located in the /usr/local/lib directory.

이 커맨드는 리눅스의 gcc컴파일러를 보고 myprogramc라는 파일을 -lm을 통해 math library를 링킹하고, /usr/local/lib의 라이브러리파일을 사용하여 컴파일하라는 명령어입니다.

The exact flags and options used to link with a library will depend on the compiler and operating system you are using.

The documentation for your specific compiler will have more information on the available flags and options.

링킹에 사용되는 정확한 플래그와 옵션은 컴파일러와 운영체제에 따라서 다르니까 니가 쓰는 컴파일러의 document보고 알아서 배워써라

 

전에 배워놓고 다까먹고 앉았노 나란녀석 븅신쉑..

 

(*2:Identifier , 식별자) 

 

Identifier, 식별자는 변수, 함수, 클래스나 다른 객체들을 구분하는데 쓰인 이름을 의미합니다. Class와 같이 사용자가 정의한 데이터인 경우도 있으며 파이썬에서만 쓰이는 개념은 아니지만 파이썬을 기준으로 다음과 같은 법칙을 가집니다.

 

- 파이썬에서 Identifier는 문자와 언더스코어('_'), 그리고 숫자(digit, 0~9의 수)만을 포함할 수 있습니다.

(Non ASCII letter를 쓰면 SyntaxError가 발생함)

- 특히 식별자의 첫문자는 숫자를 쓸 수 없습니다. 오직 대문자, 소문자, 언더스코어만 사용가능

- 대소문자도 얄짤없이 구분합니다.

- if, else, for, class와 같은 파이썬이 정해둔 몇몇 키워드들은 식별자로 사용할 수 없습니다.