经过python的基础, 你将有兴趣进一步了解Python3编程语言的更多高级主题。
本文介绍了它们。
请记住, Python完全适用于缩进, 建议你通过运行某些程序进行一些练习。使用Tab键为你的代码提供缩进。
本文分为以下五个部分:
类就像其他所有面向对象的编程语言一样, Python也支持类。让我们看一下关于Python类的几点。
- 类由关键字创建类.
- 属性是属于类的变量。
- 属性始终是公共属性, 可以使用点(.)运算符进行访问。例如:Myclass.Myattribute
# creates a class named MyClass
class MyClass:
# assign the values to the MyClass attributes
number = 0
name = "noname"def Main():
# Creating an object of the MyClass.
# Here, 'me' is the object
me = MyClass() # Accessing the attributes of MyClass
# using the dot(.) operator
me.number = 1337
me.name = "Harssh"# str is an build-in function that
# creates an string
print (me.name + " " + str (me.number))# telling python that there is main in the program.
if __name__ = = '__main__' :
Main()
输出:
Harssh 1337
方法
方法是一堆旨在在你的Python代码中执行特定任务的代码。
- 属于一个类的函数称为方法。
- 所有方法都需要’ 自’ 参数。如果你使用其他OOP语言进行编码, 则可以将” self” 视为用于当前对象的” this” 关键字。它会取消隐藏当前实例变量。” self” 的工作方式通常类似于” this” 。
- ” def” 关键字用于创建新方法。
# A Python program to demonstrate working of class
# methodsclass Vector2D:
x = 0.0
y = 0.0# Creating a method named Set
def Set ( self , x, y):
self .x = x
self .y = ydef Main():
# vec is an object of class Vector2D
vec = Vector2D()# Passing values to the function Set
# by using dot(.) operator.
vec. Set ( 5 , 6 )
print ( "X: " + str (vec.x) + ", Y: " + str (vec.y))if __name__ = = '__main__' :
Main()
输出:
X: 5, Y: 6
遗产
继承是指特定类从其基类继承要素的一种方式。基类也称为” 超类” , 而从超类继承的类称为” 子类”
![Python3中级主题快速入门](http://img.readke.com/220607/150UM949-0.jpg)
文章图片
如图所示, 派生类可以从其基类继承要素, 也可以定义自己的要素。
# Syntax for inheritanceclass derived - classname(superclass - name)
# A Python program to demonstrate working of inheritance
class Pet:
#__init__ is an constructor in Python
def __init__( self , name, age):
self .name = name
self .age = age# Class Cat inheriting from the class Pet
class Cat(Pet):
def __init__( self , name, age):
# calling the super-class function __init__
# using the super() function
super ().__init__(name, age) def Main():
thePet = Pet( "Pet" , 1 )
jess = Cat( "Jess" , 3 )# isinstance() function to check whether a class is
# inherited from another class
print ( "Is jess a cat? " + str ( isinstance (jess, Cat)))
print ( "Is jess a pet? " + str ( isinstance (jess, Pet)))
print ( "Is the pet a cat? " + str ( isinstance (thePet, Cat)))
print ( "Is thePet a Pet? " + str ( isinstance (thePet, Pet)))
print (jess.name)if __name__ = = '__main__' :
Main()
输出:
Is jess a cat? True
Is jess a pet? True
Is the pet a cat? False
Is thePet a Pet? True
Jess
迭代器
迭代器是可以迭代的对象。
- Python使用__iter __()方法返回该类的迭代器对象。
- 然后, 迭代器对象使用__next __()方法获取下一项。
- 引发StopIteration Exception时, for循环停止。
# This program will reverse the string that is passed
# to it from the main function
class Reverse:
def __init__( self , data):
self .data = http://www.srcmini.com/data
self .index = len (data)def __iter__( self ):
return selfdef __next__( self ):
if self .index = = 0 :
raise StopIteration
self .index - = 1
return self .data[ self .index]def Main():
rev = Reverse('Drapsicle' )
for char in rev:
print (char)if __name__ = = '__main__' :
Main()
【Python3中级主题快速入门】输出:
e
l
c
i
s
p
a
r
D
发电机
- 创建迭代器的另一种方法。
- 使用函数而不是单独的类
- 生成next()和iter()方法的后台代码
- 使用一个称为yield的特殊语句, 该语句保存生成器的状态并为再次调用next()时设置一个恢复点。
# A Python program to demonstrate working of Generators
def Reverse(data):
# this is like counting from 100 to 1 by taking one(-1)
# step backward.
for index in range ( len (data) - 1 , - 1 , - 1 ):
yield data[index]def Main():
rev = Reverse( 'Harssh' )
for char in rev:
print (char)
data = 'http://www.srcmini.com/Harssh'
print ( list (data[i] for i in range ( len (data) - 1 , - 1 , - 1 )))if __name__ = = "__main__" :
Main()
输出:
h
s
s
r
a
H
['h', 's', 's', 'r', 'a', 'H']
如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。
首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。
推荐阅读
- Python统计中的68-95-99.7规则
- Python 3基础快速入门介绍
- Python __iter__()和__next__()将对象转换为迭代器
- Python __import__()函数用法介绍
- 毕达哥拉斯四重奏详细介绍
- 毕达哥拉斯定理及其反函数–三角形10级数学
- Pyspark(使用Apache MLlib的线性回归)
- Java多线程中的死锁详细介绍
- 使用reduce操作的金字塔形式(先升后降)连续数组