Skip to content

Instantly share code, notes, and snippets.

@luojiyin1987
Created August 14, 2018 03:21
Show Gist options
  • Save luojiyin1987/077da440b609ba8f8a68c166503586bb to your computer and use it in GitHub Desktop.
Save luojiyin1987/077da440b609ba8f8a68c166503586bb to your computer and use it in GitHub Desktop.
Mylist
其次,如果想要在 Python shell (Python的互動介面)中能夠只利用變數名稱就展示用來表示 Mylist 的字串,光是 __str__ 還不夠,這必須要覆寫 __repr__:
class Mylist:
def __init__(self):
self._mylist=list()
def __len__(self):
return len(self._mylist)
def add(self,value):
return self._mylist.append(value)
def __str__(self):
return str(self._mylist)
def __repr__(self):
return str(self)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment