前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python数据分析(中英对照)·Classes and Object-Oriented Programming类和面向对象编程

Python数据分析(中英对照)·Classes and Object-Oriented Programming类和面向对象编程

作者头像
数媒派
发布2022-12-01 15:23:44
2970
发布2022-12-01 15:23:44
举报
文章被收录于专栏:产品优化

2.1.2: Classes and Object-Oriented Programming类和面向对象编程

Our emphasis has been and will be on functions and functional programming,but it’s also helpful to know at least something about classes and object-oriented programming. 我们的重点一直是函数和函数编程,但至少了解一些类和面向对象编程也是很有帮助的。 In general, an object consists of both internal data and methods that perform operations on the data. 通常,对象由内部数据和对数据执行操作的方法组成。 We have actually been using objects and methods all along,such as when working with building types like lists and dictionaries. 事实上,我们一直在使用对象和方法,例如在处理列表和字典之类的构建类型时。 You may find at some point that an existing object type doesn’t fully suit your needs, in which case you can create a new type of object known as a class. 在某些情况下,您可能会发现现有的对象类型并不完全满足您的需要,在这种情况下,您可以创建一种称为类的新对象类型。 Often it is the case that even if you need to create a new object type,it is likely that this new object type resembles,in some way, an existing one. 通常情况下,即使需要创建新的对象类型,该新对象类型也可能在某种程度上类似于现有对象类型。 This brings us to inheritance, which is a fundamental aspect of object-oriented programming. 这就引出了继承,这是面向对象编程的一个基本方面。 Inheritance means that you can define a new object type, a new class, that inherits properties from an existing object type. 继承意味着您可以定义一个新的对象类型,一个新的类,它继承现有对象类型的属性。 For example, you could define a class that does everything that Python’s built-in lists do, and then add an additional method or methods based on your needs. 例如,您可以定义一个类来完成Python内置列表所做的一切,然后根据需要添加一个或多个附加方法。 As a quick reminder of how we’ve been using methods so far,let’s define a list, ml, which consists of a sequence of numbers. 为了快速提醒我们到目前为止是如何使用方法的,让我们定义一个列表ml,它由一系列数字组成。 If I wanted to sort this list, I can use the sort method which is provided by the ml object, a list. 如果我想对这个列表进行排序,我可以使用由ml对象(列表)提供的排序方法。 If I now look at the contents of the list,we can see that the values have been sorted. 如果我现在查看列表的内容,我们可以看到这些值已被排序。 Let’s look at an example of how to create a new class,essentially a new type of Python object. 让我们看一个如何创建一个新类的示例,本质上是一个新类型的Python对象。 A class is defined using the class statement. 类是使用class语句定义的。 Class, MyList, and colon. 类、MyList和冒号。 The name of the class– in this case, MyList– immediately follows the word, "class". 类的名称——在本例中是MyList——紧跟在单词“class”之后。 Notice that we placed parentheses after MyList. 请注意,我们在MyList后面放了括号。 This is how Python specifies inheritance. 这就是Python指定继承的方式。 When a class is created via inheritance, the new class inherits the attributes defined by its base class, the class it is inheriting attributes from– in this case, a list. 当通过继承创建类时,新类继承由其基类定义的属性,基类是继承属性的类,在本例中是一个列表。 The so-called derived class, in this case "MyList",can then both redefine any of the inherited attributes, and in addition it can also add its own new attributes. 所谓的派生类(在本例中为“MyList”)可以重新定义任何继承属性,此外还可以添加自己的新属性。 You can think of the class statement as setting up a kind of blueprint of a new class. 您可以将class语句看作是建立一种新类的蓝图。 What that means is that it specifies the internal structure of these new types of objects, including what methods and operations they support, but it does not create any object of that type. 这意味着它指定了这些新类型对象的内部结构,包括它们支持的方法和操作,但它不创建任何该类型的对象。 When an object of a particular type is created,that object is sometimes called an "Instance" of that type. 创建特定类型的对象时,该对象有时称为该类型的“实例”。 So another way to state what I just said is that the class statement doesn’t create any instances of the class. 所以我刚才所说的另一种方式是class语句不创建任何类的实例。 This is a bit like defining a function with the def statement –the function definition specifies what the function does when called but defining a function does not, in itself, call or invoke that function. 这有点像用def语句定义函数——函数定义指定函数在被调用时做什么,但定义函数本身并不调用该函数。 That’s something you would do outside of the function’s definition. 这是在函数定义之外要做的事情。 That’s why it’s helpful to think of the class statement as defining a blueprint of the new class, a new type of Python object. 这就是为什么将class语句看作是定义新类的蓝图(一种新类型的Python对象)是有帮助的。 Our class makes use of the min and max functions, as well as the list remove method, so let’s review those briefly. 我们的类使用了min和max函数,以及list-remove方法,所以让我们简要回顾一下它们。 Here we have a list set up called x. 这里我们有一个名为x的列表。 I can use the min function to find the smallest element of the list, x–in this case, the number two. 我可以使用min函数来找到列表中最小的元素x——在本例中是数字2。 Similarly, I can use the max function to find the maximum element of the list–here, 11. 类似地,我可以使用max函数来查找列表中的最大元素——这里是11。 Let’s also remind ourselves how to remove elements from a list. 我们还要提醒自己如何从列表中删除元素。 The method "remove" does precisely that, and it takes just one argument, which is the value that you’d like to remove from the list. 方法“remove”正是这样做的,它只需要一个参数,即您希望从列表中删除的值。 One thing to note is that if the given value appears on the list multiple times, only its first occurrence is removed. 需要注意的一点是,如果给定值多次出现在列表中,则只删除其第一次出现的值。 Continuing with our list x– one of the elements in the list is number 10. 继续我们的列表x——列表中的一个元素是数字10。 So if we type, x.remove(10), and we look at the list again,we can see that the number 10 has been removed from the list. 因此,如果我们键入x.remove(10),然后再次查看列表,我们可以看到数字10已从列表中删除。 Let’s look at the class definition that I’ve just completed on my screen a bit more closely. 让我们更仔细地看看我刚刚在屏幕上完成的类定义。 You can see that it contains two def statements that I used to define functions. 您可以看到,它包含两个def语句,我使用它们定义函数。 The functions defined inside a class are known as "instance methods" because they operate on an instance of the class. 在类中定义的函数称为“实例方法”,因为它们在类的实例上操作。 By convention, the name of the class instance is called "self",and it is always passed as the first argument to the functions defined as part of a class. 按照约定,类实例的名称称为“self”,它始终作为第一个参数传递给定义为类一部分的函数。 Here we define two functions, two instance methods,which we call remove_min and remove_max. 这里我们定义了两个函数,两个实例方法,我们称之为remove\u min和remove\u max。 Looking at the remove_min function, we see that it only consists of one line. 查看remove_min函数,我们看到它只包含一行。 We first use the min function to find the smallest element of the list. 我们首先使用min函数来查找列表中的最小元素。 We then use the remove method to remove that element from the list. 然后,我们使用remove方法从列表中删除该元素。 The function "remove_max" works analogously,except that instead of the min function, we have the max function inside the parentheses. 函数“remove_max”的工作原理与此类似,只是在括号内有max函数而不是min函数。 Let’s then demonstrate the use of our newly defined class, MyList. 然后让我们演示一下新定义的类MyList的用法。 I’m going to define the Python list, x, consisting of a bunch of numbers. 我将定义Python列表x,它由一组数字组成。 I’m then going to define an object, y, which is in MyList,and it contains a copy of all of the numbers in x. 然后我将定义一个对象y,它位于MyList中,它包含x中所有数字的副本。 Remember, we can use the dir function to ask what methods are available for any given object. 请记住,我们可以使用dir函数询问对于任何给定对象有哪些方法可用。 In this case x is a standard Python list so it supports all of the methods of Python lists. 在本例中,x是一个标准的Python列表,因此它支持Python列表的所有方法。 We can then do the same for our object, y, which remember is the class we wrote ourselves. 然后我们可以对我们的对象y做同样的事情,记住它是我们自己编写的类。 Here, if you look at the last couple of lines,you’ll see that the method to remove max and remove min are now available. 在这里,如果您查看最后几行,您将看到删除max和删除min的方法现在可用。 We can use these methods, so let’s type y.remove_min,and if we then look at the contents of y, we’ll see that the number one, 我们可以使用这些方法,所以让我们键入y.remove_min,然后如果我们查看y的内容,我们将看到数字1, which was the smallest element, has disappeared. 最小的元素已经消失了。 It has been removed. 它已被移除。 If we then use the remove_max method, and we again look at the content of y,we’ll see that the largest element, number 10,has been removed from MyList. 如果我们使用remove_max方法,再次查看y的内容,我们将看到最大的元素,编号10,已经从MyList中删除。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 2.1.2: Classes and Object-Oriented Programming类和面向对象编程
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档