Python Class Inheritance and Polymorphism

In Object-Oriented Programming (OOP), when defining a class, you can inherit from an existing class. The new class is called a Subclass, and the inherited class is called a Base class, Parent class, or Super class.

Format:

SubclassName(ParentClassName):
	pass

Usage Example

class Animal(object):
    def run(self):
        print('Animal is running...')
class Dog(Animal):
    pass
class Dog1(Animal):
    def run(self):
        print('Dog is running...')
def run_twice(animal):
    animal.run()
    animal.run()
dog = Dog()
dog.run()    # Output: Animal is running...
dog1 = Dog1()
dog1.run()    # Output: Dog is running..., executes the subclass's own method
run_twice(Animal())
# Output: Animal is running...
# Output: Animal is running...
run_twice(Dog1())
# Output: Dog is running...
# Output: Dog is running...
# Add a new subclass Tortoise, then call the run_twice function, it still works
class Tortoise(Animal):
    def run(self):
        print('Tortoise is running slowly...')
run_twice(Tortoise())    # Calling run_twice function, it still works, just ensure the passed object has a run() method
# Output: Tortoise is running slowly...
# Output: Tortoise is running slowly...

class_exam

……

阅读全文

python类的继承和多态

在面向对象(OOP)程序设计中,当定义一个class的时候,可从某个现有的class继承 新的class称为子类(Subclass),而被继承的class称为基类、父类或超类(Base class、Super class)

格式:

子类名(父类名):
	pass

使用示例

class Animal(object):
    def run(self):
        print('Animal is running...') 
class Dog(Animal):
    pass   
class Dog1(Animal):
    def run(self):
        print('Dog is running...')      
def run_twice(animal):  
    animal.run()
    animal.run()
dog = Dog()
dog.run()    #输出:Animal is running...
dog1 = Dog1()
dog1.run()    #输出:Dog is running...,执行子类自己的方法
run_twice(Animal())
#输出:Animal is running...
#输出:Animal is running...
run_twice(Dog1())
#输出:Dog is running...
#输出:Dog is running...
#新追加一个子类型Tortoise,然后调用run_twice函数,依然可以运行
class Tortoise(Animal):
    def run(self):
        print('Tortoise is running slowly...')
run_twice(Tortoise())   #调用run_twice函数,依然可以运行,确保传入的对象有run()方法即可
#输出:Tortoise is running slowly...
#输出:Tortoise is running slowly...

class_exam

……

阅读全文

Process Pools, Thread Pools, and Coroutines in Python

Neither threads nor processes can be opened indefinitely; they will always consume and occupy resources. Hardware has limited capacity. While ensuring high-efficiency work, hardware resource utilization should also be guaranteed. Therefore, an upper limit needs to be set for hardware to alleviate its pressure, which led to the concept of pools.……

阅读全文

python中进程池,线程池与协程

不管是线程还是进程,都不能无限制的开下去,总会消耗和占用资源,硬件的承载能力是有限度的,在保证高效率工作的同时应该还需要保证硬件的资源占用情况,所以需要给硬件设置一个上限>来减轻硬件的压力,所以就有了池的概念……

阅读全文

Python3 print 函数用法总结

python3与python2在print函数上做的更加明确

1. 输出字符串和数字

print(“runoob”)    # 输出字符串 runoob

print(100)            # 输出数字 100

str = ‘runoob’

print(str)              # 输出变量 runoob

L = [1,2,’a’]          # 列表

print(L) [1, 2, ‘a’]

t = (1,2,’a’)           # 元组

print(t) (1, 2, ‘a’)

d = {‘a’:1, ‘b’:2}     # 字典

print(d) {‘a’: 1, ‘b’: 2}

……

阅读全文

Summary of Python3 print function usage

Python 3 makes the print function more explicit compared to Python 2.

1. Outputting Strings and Numbers

print("runoob") # Outputs string runoob

print(100) # Outputs number 100

str = 'runoob'

print(str) # Outputs variable runoob

L = [1,2,'a'] # List

print(L) [1, 2, 'a']

t = (1,2,'a') # Tuple

print(t) (1, 2, 'a')

d = {'a':1, 'b':2} # Dictionary

print(d) {'a': 1, 'b': 2}

2. Formatted Integer Output

Supports parameter formatting, similar to C language’s printf.

str = "the length of (%s) is %d" %('runoob',len('runoob'))

print(str) # the length of (runoob) is 6

Python String Formatting Symbols:

** Symbol** Description
%c Formats character and its ASCII code
%s Formats string
%d Formats signed decimal integer
%u Formats unsigned decimal integer
%o Formats unsigned octal number
%x Formats unsigned hexadecimal number (lowercase)
%X Formats unsigned hexadecimal number (uppercase)
%f Formats floating-point number, precision can be specified after decimal point
%e Formats floating-point number in scientific notation (lowercase ’e')
%E Same as %e, formats floating-point number in scientific notation (uppercase ‘E’)
%g Shorthand for %f and %e
%G Shorthand for %f and %E
%p Formats variable’s address in hexadecimal

Formatting Operator Auxiliary Directives:

……

阅读全文

Python Data Visualization - The Post-2000 Gaokao Generation

The post-2000 generation has finished their Gaokao (National College Entrance Examination), and there’s been extensive media coverage (they are the “fresh meat” generation, after all!). Many reports focused on this year’s examinee data, presenting it with stunning charts. Feeling a bit jealous about how beautiful those charts are? Do you want to try making one yourself? These charts are actually products of data visualization created with Python, so yes, you can definitely make them yourself!


Preparation

  1. Libraries

    • charts
    • pyecharts
  2. Data

    • Collected directly from Baidu.

Common Chart Types

Bar charts and line charts are frequently seen and used, so let’s start with the basics.

1. Bar Chart

# Number of Gaokao examinees
gaokao_num = [940,940,...,375]
gaokao_num.reverse()
# Number of admitted students
luqu_num = [700,705,...,221]
luqu_num.reverse()
# Admission rate
luqu_lev= [74.46,75,...,59]
luqu_lev.reverse()

import charts

options = {
    'chart'   : {'zoomType':'xy'},
    # Title
    'title'   : {'text': '2000-2017 Gaokao Data'},
    # Subtitle
    'subtitle': {'text': 'Source: edu.sina.com.cn'},
    # X-axis
    'xAxis'   : {'categories': ['2000',...,'2017']},
    # Y-axis
    'yAxis'   : {'title': {'text': 'Million people/year'}},
    }
series =  [{
    'type': 'column',
    'name': 'Number of Gaokao Examinees',
    'data': gaokao_num
},{
    'type': 'column',
    'name': 'Number of Admitted Students',
    'data': luqu_num
}
]
charts.plot(series, options=options, show='inline')

Due to a minor issue with my pyecharts setup, I used the charts library. Using pyecharts is even simpler, but I won’t repeat it here. You can check the source code if needed.

……

阅读全文

Python 数据可视化 - 00 后高考大军

00后大军高考结束了,网络上对这次高考的报道很多(毕竟00后小鲜肉嘛),很多都关注了今年的考生数据,并且用炫酷的图表展示。看着是不是有点小嫉妒,为什么图表还可以做得这么漂亮???是不是也想自己动手做一张???其实这些图表都可以用python制作出来的数据可视化的产物,所以当然是可以自己动手制作的啦

……

阅读全文

Parallelism in One Line of Python Code

Python has a somewhat notorious reputation when it comes to program parallelization. Technical issues aside, such as thread implementation and the GIL, I believe incorrect teaching guidance is the main problem. Common classic Python multithreading and multiprocessing tutorials often seem “heavy” and tend to scratch the surface without deeply exploring the most useful content for daily work.……

阅读全文

一行 Python 代码实现并行

Python 在程序并行化方面多少有些声名狼藉。撇开技术上的问题,例如线程的实现和 GIL,我觉得错误的教学指导才是主要问题。常见的经典 Python 多线程、多进程教程多显得偏”重”。而且往往隔靴搔痒,没有深入探讨日常工作中最有用的内容。……

阅读全文

最近文章

分类

标签

友情链接

其它