金点网络-全网资源,一网打尽
  • 网站首页
    • 金点部落
    • 小游戏
    • OpenAPI
    • 设计资产导航
    • 升级会员
  • 技能学习
    • 体育运动
    • 办公教程
    • 口才演讲
    • 小吃技术
    • 建站教程
    • 摄影教程
    • 棋牌教程
    • 网赚教程
      • 爆粉引流
      • 自媒体
      • 贴吧引流
  • 网站源码
    • 商城/淘客/交易
    • 小说/漫画/阅读
    • 影视/音乐/视频
    • 微信/微商/微擎
    • 理财/金融/货币
    • 模板/主题/插件
  • 游戏源码
    • 精品网单
    • 端游源码
    • 手游源码
    • 页游源码
  • 素材资料
    • 电子文档
    • 综合资料
    • 考研资料
    • 设计素材
    • 音频讲座
      • 人文艺术
      • 名师讲座
      • 说书小说
  • 软件工具
    • Windows软件
    • MacOS软件
    • Android软件
  • 寄售资源
    • 游戏源码
    • 网站源码
    • 软件源码
  • 公益服
登录/注册
  • 专享大神特权
立即开通开通会员抄底价

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

作者 : jamin 本文共6563个字,预计阅读时间需要17分钟 发布时间: 2020-10-18 共1072人阅读

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中删除。

Python 数据分析
本站所提供的部分资源来自于网络,版权争议与本站无关,版权归原创者所有!仅限用于学习和研究目的,不得将上述内容资源用于商业或者非法用途,否则,一切后果请用户自负。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源。如果上述内容资对您的版权或者利益造成损害,请提供相应的资质证明,我们将于3个工作日内予以删除。本站不保证所提供下载的资源的准确性、安全性和完整性,源码仅供下载学习之用!如用于商业或者非法用途,与本站无关,一切后果请用户自负!本站也不承担用户因使用这些下载资源对自己和他人造成任何形式的损失或伤害。如有侵权、不妥之处,请联系站长以便删除!
金点网络-全网资源,一网打尽 » Python数据分析(中英对照)·Classes and Object-Oriented Programming类和面向对象编程

常见问题FAQ

免费下载或者VIP会员专享资源能否直接商用?
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
是否提供免费更新服务?
持续更新,永久免费
是否经过安全检测?
安全无毒,放心食用
jamin

jamin 大神

下一篇
网络协议-组播介绍

相关推荐

Python数据分析(中英对照)·Objects 对象

Python数据分析(中英对照)·Objects 对象

Python数据分析(中英对照)·Random Choice 随机选择

Python数据分析(中英对照)·Random Choice 随机选择

Python数据分析(中英对照)·Examples Involving Randomness 涉及随机性的例子

Python数据分析(中英对照)·Examples Involving Randomness 涉及随机性的例子

Python数据分析(中英对照)·Dictionaries 字典

Python数据分析(中英对照)·Dictionaries 字典

Python数据分析(中英对照)·Random Walks 随机游走

Python数据分析(中英对照)·Random Walks 随机游走

标签云
Android Atom ExecutorService ForkJoin GM GM后台 GM授权后台 H5 Java Javascript Linux手工服务端 pipbestcom Python ReentrantLock synchronized ThreadLocal volatile Win一键即玩服务端 一键端 传奇 写作 创业 单机 后台 商业端 外网 安卓 安卓苹果双端 工具 手工端 手游 搭建教程 教程 数据分析 文案 游戏源码 端游 经典 网单 职场 自媒体 视频教程 详细搭建教程 运营后台 页游

近期文章

  • 回合手游【逍遥西游之繁华西游】最新整理单机一键既玩镜像服务端_Linux手工端_GM后台_教程
  • 最新整理精品回合制手游【天书奇谈3D混沌完整版】VM一键单机版_linux手工外网端_隐盟视频教程_授权GM后台_双端
  • 典藏修真页游【诸仙列传OL】最新整理Win系服务端_GM工具_详细外网搭建教程
  • MT3换皮MH【浮生若梦尊享挂机修复版】最新整理单机一键即玩镜像端_Linux手工服务端_安卓苹果双端_GM后台_详细搭建教程
  • 大话回合手游【最新引擎之缥缈西游渡劫版】最新整理Linux手工服务端_安卓苹果双端_管理后台_CDK后台_详细搭建教程_视频教程

分类

  • | wordpress插件 |
  • | wordpress模板 |
  • | 其它模板 |
  • | 帝国模板 |
  • | 织梦插件 |
  • | 织梦模板 |
  • A5源码
  • Android软件
  • APP引流
  • E语言
  • H5
  • LUA
  • QQ营销
  • SEO推广
  • Windows软件
  • 体育运动
  • 信息数据
  • 创业专题
  • 办公教程
  • 口才演讲
  • 名师讲座
  • 商城/淘客/交易
  • 小吃技术
  • 小说/漫画/阅读
  • 建站教程
  • 引流脚本
  • 影视/音乐/视频
  • 影视资源
  • 微信/微商/微擎
  • 微信小程序
  • 微信营销
  • 微擎模块
  • 手游源码
  • 技能学习
  • 抖音课程
  • 摄影教程
  • 棋牌教程
  • 模板/主题/插件
  • 游戏源码
  • 爆粉引流
  • 理财/金融/货币
  • 生活老师
  • 电商客
  • 电子文档
  • 电脑教程
  • 社群营销
  • 站长工具
  • 精品网单
  • 系统工具
  • 素材资料
  • 综合资料
  • 编程经验
  • 网站源码
  • 网络安全
  • 网赚教程
  • 网赚源码
  • 考研资料
  • 脚本/AI/智能
  • 自媒体
  • 英语学习
  • 营销软件
  • 设计素材
  • 说书小说
  • 贴吧引流
  • 软件工具
  • 软文营销
  • 逆向软件
  • 音频讲座
  • 页游源码

提供最优质的资源集合

立即加入 友好社区
金点网络-全网资源,一网打尽

新一代全网资源综合门户网(www.pipbest.com-金点网络)专注服务于互联网,提供各类最新最全的免费源码下载(PHP、ASP、JSP、.NET),更提供免费工具,免费源码下载,软件下载,素材下载,赚钱教程下载,交流论坛等网站运营相关的一切内容,为网友搜罗最有价值的网站源码下载与技术教程等服务!

服务目录
  • 金点OpenAPI
  • 金点云
  • 金点支付
友情链接
  • 数媒派
  • 国家电网
快速搜索

本站由Nice强力驱动

声明: 本站部分内容属于原创转载请注明出处 如有侵权行为请严格参照本站【版权声明】与我们联系,我们将在48小时内容进行处理!

本站部分内容属于原创转载请注明出处 如有侵权行为请严格参照本站【版权声明】与我们联系,我们将在48小时内容进行处理!
© 2016-2023 PipBest.Com - 金点网络 & 金点部落. All rights reserved 京ICP备2022005359号-1
  • 关注有礼
  • 签到
  • 客服
    官方QQ群 常见问题 FAQ

    在线客服

    点我联系

    直接说出您的需求!
    切记!带上资源连接与问题!

    工作时间: 9:30-21:30

  • 暗黑
    模式
  • 全屏
  • 投稿
    赚钱
  • 首页

  • 签到

  • 切换

  • 客服

金点网络-全网资源,一网打尽
  • 登录
  • 注册
or
or
忘记密码?
金点网络-全网资源,一网打尽
  • 网站首页 ►
    • 金点部落
    • 小游戏
    • OpenAPI
    • 设计资产导航
    • 升级会员
  • 技能学习 ►
    • 体育运动
    • 办公教程
    • 口才演讲
    • 小吃技术
    • 建站教程
    • 摄影教程
    • 棋牌教程
    • 网赚教程 ►
      • 爆粉引流
      • 自媒体
      • 贴吧引流
  • 网站源码 ►
    • 商城/淘客/交易
    • 小说/漫画/阅读
    • 影视/音乐/视频
    • 微信/微商/微擎
    • 理财/金融/货币
    • 模板/主题/插件
  • 游戏源码 ►
    • 精品网单
    • 端游源码
    • 手游源码
    • 页游源码
  • 素材资料 ►
    • 电子文档
    • 综合资料
    • 考研资料
    • 设计素材
    • 音频讲座 ►
      • 人文艺术
      • 名师讲座
      • 说书小说
  • 软件工具 ►
    • Windows软件
    • MacOS软件
    • Android软件
  • 寄售资源
    ►
    • 游戏源码
    • 网站源码
    • 软件源码
  • 公益服
×
u3** 刚刚下载了 爆款吸金文案训练

    全网资源·一网打尽

  • 金点出品,必属精品!
  • 发布原创内容,获取高额提成!
  • 我们与你共创美好数字生态!
  • 无特殊说明密码默认:pipbest.com