Skip to content

Instantly share code, notes, and snippets.

@JackInTaiwan
Last active October 19, 2019 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JackInTaiwan/4f5f6b6d5589e186bd4e8b7e21f47708 to your computer and use it in GitHub Desktop.
Save JackInTaiwan/4f5f6b6d5589e186bd4e8b7e21f47708 to your computer and use it in GitHub Desktop.
python_iteration_1.py
x = [1, 2, 3]
### Method 1: use "dir()"
print(dir(x))
# ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__',
# '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
# '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__',
# '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__',
# '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__',
# '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__',
# '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index',
# 'insert', 'pop', 'remove', 'reverse', 'sort']
### Method 2: use "hasattr()"
print(hasattr(x, '__iter__'))
# True
print(hasattr(x, '__getitem__'))
# True
print(hasattr(x, '__next__'))
# False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment