Object-Based Programming--python

本文介绍了Python面向对象编程的基础概念,包括封装、数据隐藏、抽象数据类型等,并对比了过程式编程与面向对象编程的不同之处。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<<DEITEL - Python How to Program 2002>>

Objectives

目标:

• To understand the software-engineering concepts of “encapsulation” and “data hiding.”

为了理解封装和数据隐藏的软件工程概念

• To understand the notions of data abstraction and abstract data types (ADTs).

为了理解数据抽象和抽象数据类型ADT的概念

• To create Python ADTs, namely classes.

为了建立python ADT,也即“类”

• To understand how to create, use and destroy objects of a class.

为了理解怎样去建立、使用和销毁一个类的诸多对象

• To control access to object attributes and methods.

为了控制对象属性和方法的访问

• To begin to appreciate the value of object orientation.

开始感受面向对象编程的价值

My object all sublime I shall achieve in time.

W. S. Gilbert

Is it a world to hide virtues in?

William Shakespeare, Twelfth Night

Your public servants serve you right.

Adlai Stevenson

Classes struggle, some classes triumph, others are eliminated.

Mao Zedong

This above all: to thine own self be true.

William Shakespeare, Hamlet

 

Object-oriented programming (OOP) encapsulates (i.e., wraps) data (attributes) and functions (behaviors) into components called classes.

OOP封装(即包裹)了数据(属性)和函数(行为),完事之后成为“class”

 The data and functions of a class are intimately tied together. A class is like a blueprint.

一个类中的数据和函数紧紧地拥抱在一起。一个类相当于一个"blueprint”

 排比句:

 Using a blueprint, a builder can build a house.

 Using a class, a programmer can create an object
(also called an instance).

One blueprint can be reused many times to make many houses.
One class can be reused many times to make many objects of the same class.

 Classes have a property called information hiding. This means that, although objects may know how to communicate with one another across well-defined interfaces, one object normally should not be allowed to know how another object is implemented—implementation details are hidden within the objects themselves.

Surely it is possible to drive a car effectively without knowing the details of how engines, transmissions and exhaust systems work internally.
We will see why information hiding is crucial to good software engineering.

 

对比句:

In C and other procedural programming languages, programming tends to be actionoriented; in Python, programming can be object-oriented. In procedural programming, the unit of programming is the function. In object-oriented programming, the unit of programming is the class from which objects eventually are instantiated (i.e., created).

过程式语言tends to be actionoriented,the unit of programming is the function。

面向对象语言tends to be object-oriented,the unit of programming is the class。用这一个个class实例化对象(也就是created)

 

 继续对比:

 Procedural programmers concentrate on writing functions. Groups of actions that perform some task are formed into functions, and functions are grouped to form programs.

过程式语言集中精力在函数上。行为组--这个行为组用来执行某项任务are formed into ”函数"。

Data certainly is important in procedural programming, but the view is that data exists primarily in support of the actions that functions perform. The verbs in a system specification—a document that describes the services an application should provide—help the procedural programmer determine the set of functions that will work together to implement
the system.

 数据在过程式编程中无疑是重要的,但是关键在你怎么看待数据。这里的数据存在主要是用来支持函数欲表现的行为。

Object-oriented programmers concentrate on creating their own user-defined types,called classes. Classes are also referred to as programmer-defined types. Each class contains data and the set of functions that manipulate the data. The data components of a class are called attributes (or data members). The functional components of a class are called methods (or member functions, in other object-oriented languages). The focus of attention in object-oriented programming is on classes rather than on functions. The nouns in a system specification help the object-oriented programmer determine the set of classes that will be used to create the objects that will work together to implement the system.

 

过程式语言使用的技术是“动词”,面对对象则是“名词”

 

Software Engineering Observation 7.1
A central theme of this book is “reuse, reuse, reuse.” We will carefully discuss a number of techniques for “polishing” classes to encourage reuse. We focus on “crafting valuable classes” and creating valuable “software assets.”

 

 

 

转载于:https://www.cnblogs.com/caidaodaxia/archive/2010/08/24/1806906.html

### 基于 Transformer 的算法及其应用 #### 1. 自然语言处理中的 Transformer 应用 Transformer 是一种基于注意力机制的架构,最初由 Vaswani 等人在 2017 年提出[^3]。它通过引入自注意力机制(Self-Attention Mechanism),使得模型能够并行化计算,并在序列长度较长的情况下表现优异。 在自然语言处理领域,Transformer 已经成为主流架构之一。以下是几个重要的应用场景: - **机器翻译** Transformer 被广泛应用于神经机器翻译任务中。相比于传统的循环神经网络(RNN),Transformer 提供了更高的效率和更好的性能[^3]。 - **文本生成** 文本生成是 Transformer 的另一重要应用方向。例如 GPT 系列模型利用单向自回归的方式生成高质量的文本[^1]。 - **问答系统** BERT 及其变种模型通过双向编码器结构,在问答任务中表现出色。这些模型可以理解上下文关系,从而更精准地回答问题[^1]。 ```python import torch from transformers import BertTokenizer, BertForQuestionAnswering tokenizer = BertTokenizer.from_pretrained('bert-base-uncased') model = BertForQuestionAnswering.from_pretrained('bert-large-uncased-whole-word-masking-finetuned-squad') def answer_question(question, text): inputs = tokenizer.encode_plus(question, text, add_special_tokens=True, return_tensors="pt") start_scores, end_scores = model(**inputs) all_tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0]) answer = ' '.join(all_tokens[torch.argmax(start_scores): torch.argmax(end_scores)+1]) return answer ``` --- #### 2. 计算机视觉中的 Transformer 应用 尽管 Transformer 最初设计用于处理文本数据,但它也被成功迁移到计算机视觉领域。以下是一些典型的应用场景: - **图像分类** Vision Transformers (ViT) 将图像分割成固定大小的小块,并将这些小块视为“词”,送入 Transformer 中进行特征提取和分类[^1]。 - **对象检测** DETR(End-to-End Object Detection with Transformers)是一种端到端的对象检测方法,它结合了 Transformer 编码器解码器结构来完成目标定位和类别预测的任务[^1]。 - **掩码图像建模** MIM 方法借鉴了 NLP 领域的 Masked Language Model 思想,通过对部分像素施加遮罩操作,让模型学习重建原始图像的内容[^1]。 --- #### 3. 多模态任务中的 Transformer 随着跨模态学习的发展,Transformer 在多模态任务上的潜力逐渐显现出来。例如 CLIP 模型通过联合优化文本和图像嵌入空间,实现了零样本迁移的能力;而 DALL·E 则展示了如何生成复杂的合成图片[^1]。 --- #### 4. 知识蒸馏与轻量化 为了降低 Transformer 模型的复杂度,知识蒸馏技术被广泛应用。这种方法允许较大的教师模型指导较小的学生模型的学习过程,从而使后者能够在保持较高精度的同时具备更低的推理成本[^4]。 --- ### 示例代码:使用 Hugging Face 实现简单的文本分类任务 下面是一个基于 PyTorch 和 Hugging Face Transformers 库实现的情感分析例子: ```python from transformers import pipeline classifier = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english") result = classifier("I love programming!") print(result) ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值