首页 > 编程语言 >Is Python a language that support object oriented?

Is Python a language that support object oriented?

时间:2023-03-09 18:00:52浏览次数:34  
标签:area Python support object class oriented method self

Is Python a language that support object oriented?

Yes, Python is a fully object-oriented language. In Python, everything is an object, including numbers, strings, functions, and classes. Python supports all of the fundamental features of object-oriented programming (OOP), such as encapsulation, inheritance, and polymorphism.

Python's OOP capabilities are implemented through the use of classes and objects. A class is a blueprint for creating objects, which are instances of the class. A class can have attributes (data) and methods (functions), and it can inherit attributes and methods from other classes. Objects are created by calling the constructor of a class using the syntax object_name = class_name(), and methods can be called on objects using dot notation, like object_name.method_name().

Python also provides support for multiple inheritance, which allows a class to inherit from multiple parent classes. In addition, Python supports operator overloading, which allows classes to define how they respond to certain operators (such as + or -).

Overall, Python's object-oriented capabilities make it a powerful and flexible language for building complex software systems.

 

Show me the polymorphism in Python with code sample

Polymorphism is a key feature of object-oriented programming that allows objects of different classes to be treated as if they are objects of the same class. In Python, polymorphism can be demonstrated through method overriding, where a method in a subclass has the same name and parameters as a method in its superclass, but with a different implementation.

Here is an example of polymorphism in Python:

class Shape:
    def area(self):
        pass

class Rectangle(Shape):
    def __init__(self, length, width):
        self.length = length
        self.width = width
        
    def area(self):
        return self.length * self.width

class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius
        
    def area(self):
        return 3.14 * self.radius ** 2

shapes = [Rectangle(2, 4), Circle(3)]
for shape in shapes:
    print(shape.area())

In this example, we define a superclass Shape with a method area that returns the area of the shape. We then define two subclasses, Rectangle and Circle, that inherit from Shape and override the area method with their own implementations.

In the main function, we create a list of Shape objects that includes both Rectangle and Circle objects. We then iterate over the list and call the area method on each object. Despite the fact that the area method is implemented differently in each subclass, we are able to call it in the same way on all objects of the Shape class, demonstrating polymorphism.

 

 

 

 

 

 

标签:area,Python,support,object,class,oriented,method,self
From: https://www.cnblogs.com/chucklu/p/17199462.html

相关文章

  • Python的安装
    Python官网地址:https://www.python.org/Windowshelpfile                 --------帮助文档Windowsx86-64embeddablezipfile ......
  • python: 字典的keys() values() 取得字典的键与值
    >>>attr={"class":"avdatacard","data-hash-id":"data-hash-id"}>>>attr.keys()dict_keys(['class','data-hash-id'])>>>list(attr.keys())['class','da......
  • Python爬虫初探
    准备部分0x01爬虫的简介和价值a.简介自动抓取互联网数据的程序,是基础技术之一b.价值快速提取网络中有价值的信息0x02爬虫的开发环境a.环境清单Python3.7开......
  • 利用Python为女神制作一个专属网站
     一、数据准备首先是测试图片的获取,毕竟萝卜哥当前还没有那么多女神的照片这里我使用如下网站的高清图片,嗯,各个都是大美女   抓取的代码比较简单importreque......
  • 漫谈Python魔术方法,见过的没见过的都在这里了
    漫谈Python魔术方法,见过的没见过的都在这里了就说一下,不深入假的一览提到魔术方法,学过python都应该知道一些。至少你得会__init__吧。在我之前写的博文中有很多都涉......
  • 实验1 Python开发环境使用和编程初体验
    实验任务1task1_1程序源码#task1_1print输出的几种用法#用法1:用于输出单个字符串或单个变量print('hey,u')#用法2:用于输出多个数据项,用逗号分隔print('hey','u......
  • python小题目
    1、有一个列表,去除其中的重复项a=[1,3,12,7,3,1,5,8,12,5,21,44]new_a=list(set(a)) 2、实现IloveChina输出ChinaloveIdeftest_reverse(s......
  • 实验1 Python开发环境使用和编程初体验
    print(x,y,z)print('x=%d,y=%d,z=%d'%(x,y,z))print('x={},y={},z={}'.format(x,y,z))print(f'x={x},y={y},z={z}')print(......
  • python+playwright 学习-28 定位多个元素
    前言我们一般定位到页面上唯一的元素再进行操作,有时候一个元素的属性是一样的,会定位到多个元素click方法当定位到页面唯一元素的时候,可以调用click方法<div>......
  • Python分析嵌入式日志小工具开发
    背景数据可度量,无人可值守,问题早发现功能点关键字分析自动压缩LOG自动发送邮件代码#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time    : 2022/8/2......