파이썬 코드와 관련하여 질문 드립니다.

missilerage의 이미지

제가 이러한 코드를 작성했습니다.

import socket

class IRC:
    def __init__(self, nick, server, port=6667, realname, username):
          self.nick = nick
          self.server = server

이 코드를 가지고 아래와 같은 방법으로 하려고 합니다.
(적절한 표현을 잘 모르겠습니다. 죄송합니다.)

i = IRC("missilerage", "OnAir.hanirc.org", "real", "user missile")
i.connect()

제가 간단한 예제를 작성하여 저걸 해보았는데 잘 되었습니다.

그런데 어떤 분께선 저게 안될 거라고 합니다.

저 코드만 써두고 더 나갈까 말까 고민중입니다. 제 테스트에서 발견하지 못한 문제가 있을수 있으니까요.

[/code]

hongminhee의 이미지

저도 그러고 싶을 때가 있어서, 종종 아래와 같이 작성합니다.

def __init__(self, nick, server, a, b, c=None): 
    if c == None:
        port = 6667
        realname = a
        username = b
    else:
        port = a
        realname = b
        username = c 

근데 절대 권장하지는 않습니다.

missilerage의 이미지

그렇군요.

IRC에서 제 질문을 받아주신 분에게로 부터도 부정적 답변을 받았습니다.

그래서 더욱더 호기심이 발동하는데... 왜 권장하지 않는 방법일까요?

저 코드 그대로 계속 해나가다 보면 깨달을 수 있을까요?

제가 아직 실력이 미약하여 느낌이 오질 않습니다.

Night Developer, NiDEV.

블로그 그런거 없어요.

missilerage의 이미지

class IRC:
    def __init__(self, nick, server, port=6667, realname, username):
          self.nick = nick
          self.server = server 

port=6667 이 문제가 있다는 지적을 받았습니다.

Night Developer, NiDEV.

블로그 그런거 없어요.

lifthrasiir의 이미지

missilerage wrote:
class IRC:
    def __init__(self, nick, server, port=6667, realname, username):
          self.nick = nick
          self.server = server 

port=6667 이 문제가 있다는 지적을 받았습니다.

def func(a, b=1, c=2, d): return (a, b, c, d) 이런 코드가 있다고 하면 func(3, 4, 5)에서 4가 b에 대응되는지 c에 대응되는지 알 수 없습니다. 따라서 이런 모호성을 피하기 위해 보통은 선택적 인자를 지원하는 많은 언어들에서는 선택적 인자가 맨 뒤에 오도록 되어 있거나, 위치 인자(func(3, c=4, 5) 식으로)를 지원하는 경우가 많습니다. (물론 다른 이유가 있을 지도 모릅니다.)

- 토끼군

missilerage의 이미지

def func(a, b=1, c=2, d): return (a, b, c, d)

써놓고도 묘하군요. a와 b에 각각 1을 대입할건지, b에만 1을 대입할건지...

국어에서 말하는 '중의적' 문장이 되었군요. :D

감사합니다. :)

Night Developer, NiDEV.

블로그 그런거 없어요.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.