首页 > 编程语言 >关于Python 面向对象寻值的问题. How the number be found in the OOP in Python

关于Python 面向对象寻值的问题. How the number be found in the OOP in Python

时间:2023-01-27 01:55:08浏览次数:43  
标签:Animals Python age number li How class

今天在看Python面向对象的时候看到了一个很有意思的问题

Today. When i learning the OOP in python , I found a very interesting Question that how a number be found in the parent class and the child class .

First of all, let us define a class Animals 

class Animals:
    age = 0

 Then let us define a child class 

class Dog(Animals):
    "Dog class"

 next, we just create a main method :

zhang_san= Dog()
li_si = Dog()
zhang_san.age = 1 
Dog.age = 10
print(zhang_san.age)
print(li_si.age)

 

We will get a result that the age in zhang_san is absolutely 1 .But how about the age in object li_si ?

Actually the result of li_si is 10.

So why ?

if we also set the value of age of li_si as any numbers such as 99 

The outcome will be different. 

So why ?

As the number of age is a class number , belong to the class of Animals and the dog is the child node of Animals 

When you want to find a number in python . the program will to find the child node in first , if can not find any number you want the python will countine to search according to their parent nodes. At the end of the process , if it is still could not find a number , the program will throw error .

 

标签:Animals,Python,age,number,li,How,class
From: https://www.cnblogs.com/Mzcc/p/17068467.html

相关文章

  • 利用python函数调用ffmpeg批量进行转码
    本人学习python没几天,代码也没记住,写个函数到处查笔记,东拼西凑的。累……但是最终还是搞定了。欢迎高手指导,谢谢!单个文件转码#学会如何在python调用bat文件importos,......
  • Python 中的作用域
    变量的作用域是指变量的作用范围,是程序可以访问该变量的正文区域。例如:g=123deffunction():print(g)print(g)程序总共有6行在第1行,定义了变量g在第4......
  • Python int 最大最小值
    Pythonint最大最小值若有错误还请大佬指出Answer不多说,先上答案:注:这是理论上\(\mid\):这个表示整除e.g.:\(5\mid2=2\)max32位\[2^{32\times(2^{31}-1)\mid......
  • SQL Server 2005-2008 ROW_NUMBER() 分页函数效率
    --测试数据量:2161852条declare@idatetimeset@i=GETDATE();--SQL2005-2008--开始WITHtempAS(SELECTid,title,body,ROW_NUMBER()OVER(ORDERBYid)AS'Row......
  • Python pip
    Pythonpip设置全局镜像pipconfigsetglobal.index-urlhttps://pypi.tuna.tsinghua.edu.cn/simple安装指定版本的包pipinstall[包名]==[版本号]这个方法也可......
  • 02 python初识
    Python初识一、入门基础1.第一个Python程序python代码都是编写在以.py结尾的文件中。我们随便新建一个文件,并将文件后缀名改为.py,在里面编写我们的第一个python......
  • 【图神经网络论文整理】(十)—— How Powerful are Graph Neural Networks?:GIN
    作者信息:KeyuluXu,WeihuaHu,JureLeskovec,StefanieJegelka论文来源:ComputerVisionandPatternRecognition论文地址:​​https://arxiv.org/abs/1810.00826​​本......
  • objectarx调用python注意点
    1.用conda先装好虚拟环境,为了兼容cad2010,最高只能用python3.5(已经不维护了),如果cad高版本建议python3.6+2.能用pip安装的就用pip安装,不能用pip安装的不要直接用co......
  • Serverless架构下用Python轻松实现图像分类和预测
    Serverless架构下用Python轻松实现图像分类和预测图像分类是人工智能领域的一个热门话题。通俗解释就是,图像分类是一种根据各自在图像信息中所反映的不同特征,把不同类别的......
  • Python内置函数
    5.6Python内置函数Python自带的所有内置函数如下:Python函数以上为Python3.10中所有的内置函数,其中绝大部分在前面的学习当中都已经接触过了。这里在补充介绍下没有......