Python数据分析(中英对照)·Objects 对象
1.1.2: Objects 对象
Python包含许多数据类型作为核心语言的一部分。
Python contains many data types as part of the core language.
了解Python程序中的所有数据都由对象和对象之间的关系表示,这一点很重要。
It’s important to understand that all data in a Python program is represented by objects and by relationships between objects.
这里是想法-一些对象的值可以在程序执行过程中改变。
Here is the idea – the value of some objects can change in the course of program execution.
值可以更改的对象称为可变对象,而值在创建后不可更改的对象称为不可变对象。
Objects whose value can change are said to be mutable objects,whereas objects whose value is unchangeable after they’ve been created are called immutable.
Python还包含所有Python程序都可以使用的构建函数。
Python also contains building functions that can be used by all Python programs.
Python库由所有核心元素组成,如数据类型和内置函数,但Python库的大部分由模块组成。
The Python library consists of all core elements, such as data types and built-in functions but the bulk of the Python library consists of modules.
为了能够在自己的代码中使用模块,首先需要使用import语句导入这些模块。
In order for you to be able to make use of modules in your own code,you first need to import those modules using the import statement.
Python中的每个对象都有三个特征。
Each object in Python has three characteristics.
这些特征称为对象类型、对象值和对象标识。
These characteristics are called object type, object value,and object identity.
对象类型告诉Python它所处理的对象的类型。
Object type tells Python what kind of an object it’s dealing with.
类型可以是数字、字符串、列表或其他类型。
A type could be a number, or a string, or a list, or something else.
对象值是对象包含的数据值。
Object value is the data value that is contained by the object.
例如,这可能是一个特定的数字。
This could be a specific number, for example.
最后,您可以将对象标识视为对象的标识号。
Finally, you can think of object identity as an identity number for the object.
计算机内存中的每个不同对象都有自己的标识号。
Each distinct object in the computer’s memory will have its own identity number.
大多数Python对象要么有数据,要么有函数,要么两者都有关联。
Most Python objects have either data or functions or both associated with them.
这些被称为属性。
These are known as attributes.
属性的名称跟在对象的名称后面。
The name of the attribute follows the name of the object.
这两者之间用一个点隔开。
And these two are separated by a dot in between them.
这两种类型的属性称为数据属性或方法。
The two types of attributes are called either data attributes or methods.
数据属性是附加到特定对象的值。
A data attribute is a value that is attached to a specific object.
相反,方法是附加到对象的函数。
In contrast, a method is a function that is attached to an object.
通常,一个方法对该对象执行一些函数或操作。
And typically a method performs some function or some operation on that object.
对象类型始终决定其支持的操作类型。
Object type always determines the kind of operations that it supports.
换句话说,根据对象的类型,作为程序员,您可以使用不同的方法。
In other words, depending on the type of the object,different methods may be available to you as a programmer.
最后,实例是对象的一个引用。
Finally, an instance is one occurrence of an object.
例如,可以有两个字符串。
For example, you could have two strings.
它们可能存储了不同的值,但仍然支持相同的方法集。
They may have different values stored in them,but they nevertheless support the same set of methods.
为了澄清数据属性和方法之间的区别,让我们来看看Python。
To clarify the difference between a data attribute and a method,let’s do some Python.
让我们首先导入一个名为NumPy的库,我将以np的形式导入它,这只是为了节省一些输入。
Let’s first import a library called NumPy, which I’m going to import as np and this is just to save me some typing.
我将定义两个数组。
I’m going to define two arrays.
假设我的第一个数组由数字1、3和5组成。
Let’s say my first array consists of numbers 1, 3, and 5.
我要叫它x。
I’m going to call that x.
我的第二个数组将被称为y,假设它由数字1、5和9组成。
And my second array is going to be called y and let’s say it consists of numbers 1, 5, and 9.
现在这两个对象都是NumPy数组。
Now both of these objects are NumPy arrays.
这意味着它们都支持相同的方法
That means they both support the same methods
并且具有相同的可用属性。
and have the same attributes available.
如果我想知道x中数字的平均值,
If I wanted to know the mean of the numbers in x,
我只想让Python将其返回。
I would simply ask Python to return that.
这里,我调用mean方法,它连接到对象x。
Here, I’m calling the mean method, which is connected to the object x.
我可以对对象y做同样的事情。
I can do the same exact thing for object y.
在这种情况下,它们有不同的方法,但在这两种情况下,我都可以使用平均值方法。
They turn out to have different means in this case, but in both cases the mean method is available to me.
让我们看一个数据属性的示例。
Let’s look at an example of a data attribute.
如果我键入x.shape,Python会告诉我这个数组包含三个数字。
If I type x.shape, Python is telling me that this array contains three numbers.
我同样可以问,y中有多少元素?
I can similarly ask, how many elements do I have in y?
答案是一样的。
And the answer is the same.
y中嵌入了三个元素。
There are three elements embedded in y.
注意,在第一种情况下,当我要求Python计算一个值时,
Notice in the first case, when I ask Python to compute a value,
属性名称后面有括号。
I have parentheses following the name of the attribute.
这意味着平均值是一个函数。
That means mean is a function.
相反,当我要求Python返回数组的形状时,
In contrast, when I ask Python to return the shape of the array,
“形状”一词后面没有括号。
I don’t have parentheses following the word “shape”.
这意味着形状是一个数据属性。
That means shape is a data attribute.
金点网络 » Python数据分析(中英对照)·Objects 对象
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
- 是否提供免费更新服务?
- 持续更新,永久免费
- 是否经过安全检测?
- 安全无毒,放心食用