Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1745614
  • 博文数量: 335
  • 博客积分: 4690
  • 博客等级: 上校
  • 技术积分: 4341
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-08 21:38
个人简介

无聊之人--除了技术,还是技术,你懂得

文章分类

全部博文(335)

文章存档

2016年(29)

2015年(18)

2014年(7)

2013年(86)

2012年(90)

2011年(105)

分类: Python/Ruby

2011-06-12 23:01:47

2.4. Everything Is an Object

所有的东西都是对象

In case you missed it, I just said that Python functions have attributes, and that those attributes are available at runtime.

A function, like everything else in Python, is an object.

如果你忘了这点,我只想说在Python中函数也有属性,这些属性在运行时是可用的.Python中所有的东西一样,函数也是对象.

Open your favorite Python IDE and follow along:

现在打开你最喜欢的Python开发工具,并进行如下操作:

Example 2.3. Accessing the buildConnectionString Function's doc string

2.3 获取函数buildConnectionString 的文档注释

  1. >>> import odbchelper 
  2. >>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}
  3. >>> print odbchelper.buildConnectionString(params) 
  4. server=mpilgrim;uid=sa;database=master;pwd=secret
  5. >>> print odbchelper.buildConnectionString.__doc__ 
  6. Build a connection string from a dictionary
  7.  
  8. Returns string.

1

The first line imports the odbchelper program as a module -- a chunk of code that you can use interactively, or from a larger Python program. (You'll see examples of multi-module Python programs in Chapter 4.) Once you import a module, you can reference any of its public functions, classes, or attributes. Modules can do this to access functionality in other modules, and you can do it in the IDE too. This is an important concept, and you'll talk more about it later.

第一行讲odbchelper程序作为一个模块导入---一段你可以交互使用的代码,或者你也可以导入 更大的Python程序(在第四章,你将看到多模块组成的程序)。一旦你引入了一个模块,你就可以调用该模块中任意的公共函数,类,或者属性。同样的,模块可以采用的同样的方法调用其它模块中的函数,在集成开发环境中你同样可以这样做。这是一个非常重要的概念,稍后我们会继续讨论。

 

2

When you want to use functions defined in imported modules, you need to include the module name. So you can't just say buildConnectionString; it must beodbchelper.buildConnectionString. If you've used classes in Java, this should feel vaguely familiar.

当你想调用导入模块中的函数时,你需要在调用的时候包含模块名称。因此,你不能直接调用buildConnectionString;而是odbchelper.buildConnectionString;如果你使用过java中的类,这个概念对你来说有点类似。

 

3

Instead of calling the function as you would expect to, you asked for one of the function's attributes, __doc__.

在本例中,我们不像你希望的那样调用函数,而是调用函数的属性:_doc_

 

 

 

 

import in Python is like require in Perl. Once you import a Python module, you access its functions with module.function; once you require a Perl module, you access its functions with module::function.

Python中的Import就如同perl中的require。一旦你导入了一个python模块,你就可以使用模块名+函数名的方式调用该函数;在Perl中一旦你调用 一个模块,你就可以以如下方式模块名+函数名来调用该函数

 

 

2.4.1. The Import Search Path

导入模块的搜索路径

Before you go any further, I want to briefly mention the library search path. Python looks in several places when you try to import a module. Specifically, it looks in all the directories defined in sys.path. This is just a list, and you can easily view it or modify it with standard list methods. (You'll learn more about lists later in this chapter.)

在继续前进之前,有必要简单的介绍一下函数库搜搜路径。当你导入一个模块的时候,Python尝试好几个路径。特别是,它将搜索定义在sys.path变量中的所有目录。该变量只是一个目录列表,这样通过标准的列表操作方法,你就能很轻松的进行查看甚至修改该变量(在本章的后半部分你将能了解到更多的内容)。

Example 2.4. Import Search Path

导入搜索路径

  1. >>> import sys 
  2. >>> sys.path 
  3. ['', '/usr/local/lib/python2.2', '/usr/local/lib/python2.2/plat-linux2',
  4. '/usr/local/lib/python2.2/lib-dynload', '/usr/local/lib/python2.2/site-packages',
  5. '/usr/local/lib/python2.2/site-packages/PIL', '/usr/local/lib/python2.2/site-packages/piddle']
  6. >>> sys 
  7. <module 'sys' (built-in)>
  8. >>> sys.path.append('/my/new/path')

1

Importing the sys module makes all of its functions and attributes available.

导入sys模块,从而是该模块所包含的所有函数和属性都可用

2

sys.path is a list of directory names that constitute the current search path. (Yours will look different, depending on your operating system, what version of Pythonyou're running, and where it was originally installed.) Python will look through these directories (in this order) for a .py file matching the module name you're trying to import.

Sys.path是一个目录名称所组成的列表,它包含了当前的搜索路径(你可能回看到不同的结果,依赖于你的操作系统,你所运行python的版本以及python的初始安装目录。

3

Actually, I lied; the truth is more complicated than that, because not all modules are stored as .py files. Some, like the sys module, are "built-in modules"; they are actually baked right into Python itself. Built-in modules behave just like regular modules, but their Python source code is not available, because they are not written inPython! (The sys module is written in C.)

实际上,我说的并不全部都是事实,实情远比现在复杂,不是所有的python模块都是以.py结尾的。某些模块,比如一些系统模块如sys,为内置模块;他们本身是同python所绑定的。内置模块就如同 标准模块,但是这些模块的python源代码是不可用的,因为这些模块本身不是采用python实现(系统模块是用C实现的)。

4

You can add a new directory to Python's search path at runtime by appending the directory name to sys.path, and then Python will look in that directory as well, whenever you try to import a module. The effect lasts as long as Python is running. (You'll talk more about append and other list methods in Chapter 3.)

通过将目录名称添加到sys.path,python搜索路径增加一个运行时的搜索目录.这样当你在尝试导入一个目录的时候,python将会搜索该目录。只要Python在运行,这种作用一直存在。(在第三章将讨论append以及更多的列表方法)

2.4.2. What's an Object?

什么是对象

Everything in Python is an object, and almost everything has attributes and methods. All functions have a built-in attribute __doc__, which returns the doc string defined in the function's source code. The sys module is an object which has (among other things) an attribute called path. And so forth.

Python中所有的东西都是对象,几乎每一个对象都包含属性和方法,所有的函数都具有一个内置属性 _doc_,该属性返回函数源代码中所定义的文档化字符串。Sys 也是一个对象,它还包含有一个名为Path的属性。

Still, this begs the question. What is an object? Different programming languages define “object” in different ways. In some, it means that all objects must have attributes and methods; in others, it means that all objects are subclassable. In Python, the definition is looser; some objects have neither attributes nor methods (more on this in Chapter 3), and not all objects are subclassable (more on this in Chapter 5). But everything is an object in the sense that it can be assigned to a variable or passed as an argument to a function (more in this in Chapter 4).

现在我们来讨论开始提出的问题,什么是对象。不同的语言采用不同的方式来定义“对象”。在一些程序语言中,所有的对象都必须包含属性和方法;而在其它语言中,它必须是可继承的。在python中,对象的定义比较松散,有些对象既没有属性也没有方法(更懂讨论在第三章讨论),同时并不是所有的对象都可以被子类化(更多内容将在第五章讨论)。但是所有的东西都是对象,在这个意义上,对象是可以赋值给变量,或者作为函数的形参进行传递(更多内容将在第4章讨论)。

This is so important that I'm going to repeat it in case you missed it the first few times: everything in Python is an object. Strings are objects. Lists are objects. Functions are objects. Even modules are objects.

为了防止刚开始的几次你忘记它,我不得不重复强调,在python中所有的事物都是对象,这个概念非常重要。字符串是对象,列表是对象,函数时对象,甚至模块都是对象。


阅读(970) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~