如何撰写Python编程进阶学习心得笔记?
- 内容介绍
- 文章标签
- 相关推荐
本文共计796个文字,预计阅读时间需要4分钟。
@[TOC]
1.Python 中的面向对象
Python 中的面向对象编程(OOP)提供了更底层的抽象和更灵活的设计。Python 中的类和对象比 Java 中的类和对象更加灵活,因为 Python 支持动态类型和动态绑定。在 Python 中,一切皆对象,这意味着函数、模块、数字、字符串等都是对象。Python 的类和对象比 Java 的类和对象更加强大,因为它们可以动态地添加属性和方法。
与 Java 相比,Python 中的面向对象更加灵活,因为:- Python 支持动态类型,而 Java 是静态类型。- Python 支持动态绑定,而 Java 是静态绑定。- Python 的类可以动态地添加属性和方法,而 Java 的类在编译时就已经确定。
@[TOC]
1. python 一切皆对象
1.1 type 、object 和 class 的关系
list、str、dict、tuple、type都继承自object,所以object是最底层的(所有类)基类
type是自身的一个实例(通过指针实现),而object、list、str、dict、tuple都是type的对象,所以type创建所有对象
type -> str -> ’abc’所以 type 可以生成 class(类), class 生成 object(对象)
s = 'abc' print(type(s)) # <class 'str'> print(type(str)) # <class 'type'>stu = Student()print(type(stu)) # <class 'main.Student'>print(type(Student)) # <class 'type'>
4. type -> Student -> stu ```python class Student: pass #Student继承了最顶层的object同时Student又是type的对象 Student.__bases__ # <class 'object',> print(type(Student)) # <class 'type'> #type是自身的对象,object是type的对象 print(type(type)) # <class 'type'> print(type(object)) # <class 'type'> #type继承了object类,最顶层的object的基类为空 print(type.__bases__) # <class 'object'> print(object.__bases__) # ()1.2 python中对象的三个特征
2. 魔法函数
2.1 什么是魔法函数?
所谓魔法函数(Magic Methods),是Python的一种高级语法,允许你在类中自定义函数(函数名格式一般为__xx__),并绑定到类的特殊方法中。比如在类A中自定义__str__()函数,则在调用str(A())时,会自动调用__str__()函数,并返回相应的结果。- 注意
双下划线开头,如:init、getitem不要尝试自己定义魔法函数用于增强类的特性独立存在,定义后不需要显示调用
2.2 python中的魔法函数一览
未完待续 ~
本文共计796个文字,预计阅读时间需要4分钟。
@[TOC]
1.Python 中的面向对象
Python 中的面向对象编程(OOP)提供了更底层的抽象和更灵活的设计。Python 中的类和对象比 Java 中的类和对象更加灵活,因为 Python 支持动态类型和动态绑定。在 Python 中,一切皆对象,这意味着函数、模块、数字、字符串等都是对象。Python 的类和对象比 Java 的类和对象更加强大,因为它们可以动态地添加属性和方法。
与 Java 相比,Python 中的面向对象更加灵活,因为:- Python 支持动态类型,而 Java 是静态类型。- Python 支持动态绑定,而 Java 是静态绑定。- Python 的类可以动态地添加属性和方法,而 Java 的类在编译时就已经确定。
@[TOC]
1. python 一切皆对象
1.1 type 、object 和 class 的关系
list、str、dict、tuple、type都继承自object,所以object是最底层的(所有类)基类
type是自身的一个实例(通过指针实现),而object、list、str、dict、tuple都是type的对象,所以type创建所有对象
type -> str -> ’abc’所以 type 可以生成 class(类), class 生成 object(对象)
s = 'abc' print(type(s)) # <class 'str'> print(type(str)) # <class 'type'>stu = Student()print(type(stu)) # <class 'main.Student'>print(type(Student)) # <class 'type'>
4. type -> Student -> stu ```python class Student: pass #Student继承了最顶层的object同时Student又是type的对象 Student.__bases__ # <class 'object',> print(type(Student)) # <class 'type'> #type是自身的对象,object是type的对象 print(type(type)) # <class 'type'> print(type(object)) # <class 'type'> #type继承了object类,最顶层的object的基类为空 print(type.__bases__) # <class 'object'> print(object.__bases__) # ()1.2 python中对象的三个特征
2. 魔法函数
2.1 什么是魔法函数?
所谓魔法函数(Magic Methods),是Python的一种高级语法,允许你在类中自定义函数(函数名格式一般为__xx__),并绑定到类的特殊方法中。比如在类A中自定义__str__()函数,则在调用str(A())时,会自动调用__str__()函数,并返回相应的结果。- 注意
双下划线开头,如:init、getitem不要尝试自己定义魔法函数用于增强类的特性独立存在,定义后不需要显示调用
2.2 python中的魔法函数一览
未完待续 ~

