Python 질문있습니다.

saelly의 이미지

1. 첫번째 질문

# -*- coding: utf-8 -*- 이것과

# encoding=utf-8 이것...

위의 두개의 차이점이 무엇인가요??

똑같은 역할을 해주는 것인가요?

------------------------------------------------------

2. 두번째 질문

파이썬 스크립트 맨 밑에
if __name__=="__main__": => 이것을 해주는 정확한 이유는 무엇인가요?

-------------------------------------------------------

질문이 많아서 송구스럽습니다... 부디 속시원히 답변좀 달아주셔요.. 그럼 좋은 하루 되세요^^

세이군의 이미지

첫번째 질문에 대한 답변
# -*- coding: utf-8 -*-
=> 현재 파일은 UTF-8로 작성되어 있음.

# encoding=utf-8 이것...
=> 주석(아무 의미 없음)

두번째 질문에 대한 답변
그 문장은 Entry Point 입니다.
파이썬은 스크립트 언어라서 위에서부터 순차적으로 실행합니다. 그리고 C/C++과 같은 고정된 시작위치가 없습니다.
그렇기 때문에 클래스와 함수로만 되어 있는 파일은 아무리 실행하려 해도 결과가 나오지 않습니다. 어디에서 시작할 지 모르니까요.

그래서 여기가 이 파일의 시작위치다 라는 것을 알려주는 표식이 필요한데 이 표식이 본문에 적어주신 코드입니다.

if __name__=="__main__":
  main()

이런 식으로 작성합니다.
익명 사용자의 이미지

그런데 님아 궁금한 것이 #encoding=utf-8 매직 코멘트 같은데 아닌가여>?

이거 안쓰고 한글이 있는 스크립트를 실행하면, 에러가 납니다. 그런데 이거 써주고 실행하면 에러가 나지 않아요

snowall의 이미지

님아는 반말입니다

피할 수 있을때 즐겨라! http://melotopia.net/b

이응준의 이미지

# -*- coding: utf-8 -*-
# encoding=utf-8

둘 다 될 것 같네요. 정규식 coding[:=]\s*([-\w.]+) 에 매칭되기만 하면 되거든요.

자세한 건 여기에: http://www.python.org/dev/peps/pep-0263/

익명 사용자의 이미지

파이썬 코드는 직접 사용되기도 하고
다른 파이썬 코드가 import하여 사용할 수도 있습니다.

if __name__=="__main__": 부분은
자신이 직접 수행될때 수행되는 부분이고

다른 파이썬 코드가 import했을때는
수행이 안되는 부분입니다.

익명 사용자의 이미지

글쿤요 답변 감사합니다~^^

Gethoper의 이미지

http://stackoverflow.com/questions/419163/what-does-if-name-main-do

The simplest explanation (imho) is the following:
 
Create the following files.
 
# a.py
import b
and
 
# b.py
print "Hello World from %s!" % __name__
 
if __name__ == '__main__':
    print "Hello World again from %s!" % __name__
Running them will get you this output:
 
$ python a.py
Hello World from b!
 
$ python b.py
Hello World from __main__!
Hello World again from __main__!

http://www.python.org/dev/peps/pep-0263/

To define a source code encoding, a magic comment must
    be placed into the source files either as first or second
    line in the file, such as:
 
          # coding=<encoding name>
 
    or (using formats recognized by popular editors)
 
          #!/usr/bin/python
          # -*- coding: <encoding name> -*-
 
    or
 
          #!/usr/bin/python
          # vim: set fileencoding=<encoding name> :

수고하세요
익명 사용자의 이미지

와~~ 감사합니다^^

그리고 질문전에 인터넷 검색 많이 해본 후 , 질문 올리겠습니다

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.