Skip to content
Snippets Groups Projects
Commit 2c6a74f7 authored by Vermaat's avatar Vermaat
Browse files

Objects

parent 23641c20
No related branches found
No related tags found
No related merge requests found
...@@ -2841,7 +2841,7 @@ ...@@ -2841,7 +2841,7 @@
"List comprehensions\n", "List comprehensions\n",
"---\n", "---\n",
"\n", "\n",
"Similar to mathematical set notation (e.g., $\\\\{ x ~|~ x \\in \\mathbf R \\land x > 0\\\\}$), we can create lists." "Similar to mathematical set notation (e.g., $\\{ x ~|~ x \\in \\mathbf R \\land x > 0\\}$), we can create lists."
] ]
}, },
{ {
...@@ -2969,7 +2969,94 @@ ...@@ -2969,7 +2969,94 @@
"Everything is an object\n", "Everything is an object\n",
"---\n", "---\n",
"\n", "\n",
"In Python, all values are objects. Objects have properties and methods. You can explore them using `dir(obj)`, or by typing `obj.<tab>` in the IPython interpreter." "* In Python, all values are objects.\n",
"* Objects can have properties and methods. \n",
"* You can explore them using `dir(o)`, or by typing `o.<tab>` in the IPython interpreter."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dir('abc')[-10:]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 12,
"text": [
"['rstrip',\n",
" 'split',\n",
" 'splitlines',\n",
" 'startswith',\n",
" 'strip',\n",
" 'swapcase',\n",
" 'title',\n",
" 'translate',\n",
" 'upper',\n",
" 'zfill']"
]
}
],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"help('abc'.split)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Help on built-in function split:\n",
"\n",
"split(...)\n",
" S.split([sep [,maxsplit]]) -> list of strings\n",
" \n",
" Return a list of the words in the string S, using sep as the\n",
" delimiter string. If maxsplit is given, at most maxsplit\n",
" splits are done. If sep is not specified or is None, any\n",
" whitespace string is a separator and empty strings are removed\n",
" from the result.\n",
"\n"
]
}
],
"prompt_number": 17
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"'abc abc abc'.split()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 16,
"text": [
"['abc', 'abc', 'abc']"
]
}
],
"prompt_number": 16
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Properties and methods that start and end with `__` have a special meaning and are normally not used directly."
] ]
}, },
{ {
...@@ -2984,9 +3071,37 @@ ...@@ -2984,9 +3071,37 @@
"===\n", "===\n",
"\n", "\n",
"Classes\n", "Classes\n",
"---" "---\n",
"\n",
"* You can define your own types of objects with classes.\n",
"* The constructor method is called `__init__`.\n",
"* Initialize properties in the constructor.\n",
"* Every method recieves the object (class instance) as its first argument."
] ]
}, },
{
"cell_type": "code",
"collapsed": false,
"input": [
"class Oracle:\n",
" def __init__(self):\n",
" self.answer = 42\n",
" def ask_question(self, question):\n",
" print 'Q:', question\n",
" print 'A:', self.answer"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": { "metadata": {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment