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 self as the first parameter

  • (self is 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 self

  • If 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

source click 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 name

    • Use CapWords for class names, but lower_with_under.py for module names.

  • Guidelines for naming

  • Type
    Public
    Internal

    Packages

    lower_with_under

    Modules

    lower_with_under

    _lower_with_under

    Classes

    CapWords

    _CapWords

    Exceptions

    CapWords

    Functions

    lower_with_under()

    _lower_with_under()

    Global/Class Constants

    CAPS_WITH_UNDER

    _CAPS_WITH_UNDER

    Global/Class Variables

    lower_with_under

    _lower_with_under

    Instance Variables

    lower_with_under

    _lower_with_under (protected)

    Method Names

    lower_with_under()

    _lower_with_under() (protected)

    Function/Method Parameters

    lower_with_under

    Local 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?