{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 什么是对象?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "在 `Python` 中,几乎所有的东西都是对象。\n", "\n", "整数是对象:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = 257" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "53187032L" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`b` 和 `a` 是同一个对象:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "b = a" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "53187032L" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(b)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "53186960L" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = 258\n", "id(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "函数:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def foo():\n", " print 'hi'" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "function" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(foo)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "63632664L" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(foo)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`type` 函数本身也是对象:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "type" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(type)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "506070640L" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(type)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "只有一些保留的关键词不是对象:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "error", "traceback": [ "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m id(if)\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "id(if)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "error", "traceback": [ "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m id(+)\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "id(+)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }