Python Tutorial - Tips
if __name__=="_main_"
๋ชจ๋์ด ์ง์ ์คํ๋์๋์ง ํน์ import ๋์๋์ง ์๋์ง ํ๋จํ ๋ __name__ ๋ณ์์ ๊ฐ์ ์ฌ์ฉํฉ๋๋ค.
์ผ๋ฐ์ ์ผ๋ก, ๋ชจ๋์ ์ง์ ์คํ๋๊ฑฐ๋ ๋ค๋ฅธ ๋ชจ๋์์ import ๋์ด ์ฌ์ฉ๋ฉ๋๋ค. ๋ง์ฝ ๋ชจ๋์ด ์ง์ ์คํ๋๋ฉด, __name__ ๋ณ์๋ ๋ฌธ์์ด"__main__"์ด ํ ๋น๋ฉ๋๋ค. ๋ฐ๋๋ก, ๋ชจ๋์ด import ๋์ด ์ฌ์ฉ๋ ๋๋,__name__๋ณ์๋ ํด๋น ๋ชจ๋์ ์ด๋ฆ(ํ์ผ๋ช
)์ด ํ ๋น๋ฉ๋๋ค.
๋ฐ๋ผ์, __name__ ๋ณ์์ ๊ฐ์"__main__"๊ณผ ๋น๊ตํ๋ฉด ํ์ฌ ๋ชจ๋์ด ์ง์ ์คํ๋๋์ง(import ๋๋์ง)๋ฅผ ํ๋จํ ์ ์์ต๋๋ค. ๋ฐ๋ผ์ ์ฝ๋๋ฅผ ifname == "main"๋ก๊ฐ์ธ๋ฉด, ํด๋น ํ์ผ์ด ๋ชจ๋๋ก ์ฌ์ฉ๋ ๋๋ ์คํ๋์ง ์๊ณ , ์ง์ ์คํ๋ ๋๋ง ์คํ๋ฉ๋๋ค.
์ฅ์ :
๋ชจ๋๋ก ์ฌ์ฉ๋ ๋๋ ์ฝ๋๊ฐ ์คํ๋์ง ์์ผ๋ฏ๋ก, ๋ค๋ฅธ ๋ชจ๋์์ ํด๋น ๋ชจ๋์ import ํ ๋ ๋ฐ์ํ ์ ์๋ ๋ถ์์ฉ์ ๋ฐฉ์งํ ์ ์์ต๋๋ค.
๋ชจ๋์ ๊ฐ๋ฐํ ๋, ํ ์คํธ ์ฝ๋๋ฅผ ์ถ๊ฐํ๊ณ ์ถ์ ๋,
if name == "main":๊ตฌ๋ฌธ์ ํ์ฉํ์ฌ, ํด๋น ๋ชจ๋์ ์ง์ ์คํํ ๋๋ง ํ ์คํธ ์ฝ๋๊ฐ ์คํ๋๋๋ก ํ ์ ์์ต๋๋ค.
# calc.py
def add(a, b):
return a + b
def mul(a, b):
return a * b
print('calc.py __name__:', __name__) # __name__ ๋ณ์ ์ถ๋ ฅ
if __name__ == '__main__': # ํ๋ก๊ทธ๋จ์ ์์์ ์ผ ๋๋ง ์๋ ์ฝ๋ ์คํ
print(add(10, 20))
print(mul(10, 20))
# cal_main.py
import calc
print('calc_main.py __name__:', __name__) # __name__ ๋ณ์ ์ถ๋ ฅ
calc.add(50, 60)Class in Python
Instance methods
Called using objects
Must have
selfas the first parameter(
selfis another python term. We can use self to access any data or other instance methods which resides in that class. These cannot be accessed without using self)
Initializer Method
must be called
__init__()~~(~~double underscore is used by python runtime)The first parameter is
selfIf the initializer method is present, the constructor calls
__init__()
Super() to inherit all the methods and properties from another class:
Inherits all the method, properties of Parent or sibling class
> **_The underscore prefix in a variable/method name is meant as a _hint to another programmer that a variable or method starting with a single underscore is intended only for internal use. This convention is defined in PEP 8.
NP Array
Reshaping arrays
.shape
input_tensor.shape[-1] # get value of the last index of shape
source: read here

Stacking 2D data to 3D data
Documenting your code in Python (docstring)
An official specification on how you should format your docstrings called PEP 0257, based on reStructuredText (reST)
For Google style guide for C++, read here
Google format
Names to Avoid
dashes (
-) in any package/module nameUse CapWords for class names, but lower_with_under.py for module names.
Guidelines for naming
- TypePublicInternal
Packages
lower_with_underModules
lower_with_under_lower_with_underClasses
CapWords_CapWordsExceptions
CapWordsFunctions
lower_with_under()_lower_with_under()Global/Class Constants
CAPS_WITH_UNDER_CAPS_WITH_UNDERGlobal/Class Variables
lower_with_under_lower_with_underInstance Variables
lower_with_under_lower_with_under(protected)Method Names
lower_with_under()_lower_with_under()(protected)Function/Method Parameters
lower_with_underLocal Variables
lower_with_under Example Google style docstrings
This module demonstrates documentation as specified by the
Google Python Style Guide
Last updated
Was this helpful?