美文网首页
Python类和对象笔记

Python类和对象笔记

作者: 枯藤疯鸦 | 来源:发表于2019-11-03 17:39 被阅读0次

小白笔记
仅记录常规操作中较为不熟悉的操作类型

1、类可以理解为一种自定义的数据类型,它定义了这一类对象普遍具有的或需要的属性(变量)、方法(自定义函数)

例如:学生是一个自定义类(可理解为特殊数据类型),学号、姓名、班级信息、授课老师等是其普遍性的属性,成绩记录工具、期末评价工具等是其普遍有的方法(工具)
具体学生:张三、李四,就是这个类的实例对象

2、类用 def __ init __() 方法来定义 对象 的属性,所有对象的方法(类定义中的自定义函数)的第一个参数均为:self

class Student(object):
     def __init__(self, name, score):
         self.name = name 
         self.score = score
 
 student1 = Student()
 studen1.name = "Tony"

如果要内部属性不被外部访问,可以把属性的名称前加上两个下划线 __,在Python中,实例的变量名如果以 __ 开头,就变成了私有变量(private),只有内部可以访问,外部不能访问

私有方法声明:用两个下划线开头的方法名(函数名)def __XX
注意的是,在Python中,变量名类似 __ xxx __ 的,也就是以双下划线开头,并且以双下划线结尾的,是特殊变量,特殊变量是可以直接访问的,不是private变量

3、在方法中引用对象的属性,需要用:self.属性名 的形式

 class Student(object):
     ...
     def get_grade(self):
         if self.score >=90: 
             return  'A' 
         elif self.score >=60: 
             return 'B' 
         else: 
             return 'C'

4、整个类的 属性 定义,直接在类定义中定义一个变量并复制即可,无需用__ init __方法

 class Student(object): 
     school_name ='Beijing Primary School'

5、类定义中 __ slots __ 变量,用来限定类属性数量

 class Student(object): 
     __slots__ = ('name', 'age')    # 用tuple定义允许绑定的属性名称

如果定义一个 Student 实例,限制不允许向该实例添加除 name 、age 以外的属性了

 >>> s = Student()    # 创建新的实例 
 >>> s.name ='Michael'    # 绑定属性'name'
 >>> s.age =25    # 绑定属性'age' 
 >>> s.score =99    # 绑定属性'score' 
 Traceback (most recent call last):  
      File"<stdin>", line1,in<module> 
 AttributeError:'Student'object has no attribute'score'

使用 __ slots __ 定义的属性仅对当前类实例起作用,对继承的子类是不起作用,除非在子类中也定义 __ slots __ ,这样,子类实例允许定义的属性就是自身的 __ slots __ 加上父类的 __ slots __

6、装饰器@property 对某些类对象的属性来说,可能要一定的范围限定如Student类中的分数 score 属性就有范围限制,所以我们就需要在类中定义一些方法来对其进行设置、验证和输出

 class Student(object): 
     def __init__(self, name, score): 
         self.name = name 
         self.score = score     

     def get_score(self): 
         return self._score 

     def set_score(self, value): 
         if not is instance(value, int): 
             raise ValueError('score must be an integer!') 
         if value < 0 or value  > 100: 
             raise ValueError('score must between 0 ~ 100!')  
          self._score = value 

 student1 = Student()
 student1.get_score()
 student1.set_score(90)

那我们在对属性 score 进行读取、设置时,就无法像属性 name 那样直接读取或通过赋值实现,需要通过 student1.get_score()、student1.set_score(90) 来实现,整个过程比较麻烦。
为了便于属性设置,我们可以通过装饰器@property 将这麻烦的过程省略掉,实现将方法变为按属性调用的过程

 class Student(object): 
     ……
     @property
     def score(self):
         return self._score

     @score.setter
     def score(self, value):
         if not is instance(value, int):
             raise ValueError('score must be an integer!')
         if value < 0 or value  > 100:
             raise ValueError('score must between 0 ~ 100!') 
         self._score = value
 
 student1 = Student()
 student1.score = 90
 student1.score

这样我们就可以直接通过属性调用的方式对 score 进行设置或读取:student1.score = 90、student1.score
@property 装饰方法后(读取),@property本身又创建了另外两个装饰器@score.setter(设置) 和 @score.deleter(删除),这两个装饰器可以用也可以不用

7、@classmethod 装饰器,严格说是一种类型声明。它声明了该方法为类的方法,非对象方法,大部分用于实例化前的数据清洗、转换、判断等用途。
我们新建一个日期类,在给出年、月、日的数字信息时,按特定格式输出

 class Demo:
     def __init__(self,year,month,day):
         self.year= year
         self.month= month
         self.day= day

     def out_date(self):
         return "year:%d, month:%d, day:%d" % (self.year,self.month,self.day)

 demo1= Demo(2017,12,25)
 print(demo1.out_date())

 #  year:2017, month:12, day:25

但"2017-5-6"形式的日期,就无法利用这个类来输出特定格式的日期,这就需要我们在遇到这种字符格式时,现对它进行一次处理,这时我们就需要要一个方法来实现这个处理
而这个方法并不少这一类对象普遍需要的方法,且与对象属性有冲突,所以用类方法的形式来实现

 class Demo:
     def __init__(self,year,month,day):
         self.year= year
         self.month= month
         self.day= day

     def out_date(self):
         return "year:%d, month:%d, day:%d" % (self.year,self.month,self.day)
 
     @classmethod
     def pre_out(cls,date_string):
         year, month, day= map(int,date_string.split("-") 
         return cls(year, month, day)    # 返回一个初始化的类,对类进行实例化
 
 demo2= Demo.pre_out('2017-5-6')    # 不需要实例化类就可以被类本身调用
 print(demo2.out_date())    # 实例化调用
 
 # year:2017, month:5, day:6

8、@staticmethod 装饰器,它严格上来说是一个普通的函数,不强制要求传递参数(不代表没有参数),与类和实例都没有所谓的绑定关系,所以没有要求第一个参数必须是cls或self
它其实是对类的一些逻辑关联的处理,比如对输入的日期进行合法性判断(day<=31,mouth<=12),这种判断可以在类定义外解决, 但这样同样会扩散类内部的代码,造成维护困难。所以我们将它放在类定义中

 class Demo:
     def __init__(self,year,month,day):
         self.year= year
         self.month= month
         self.day= day
 
     def out_date(self):
         return "year:%d, month:%d, day:%d" % (self.year,self.month,self.day)

     @staticmethod
     def is_date_valid(date_as_string1):      # 注意参数,跟类没有严格的绑定关系
         year, month, day= map(int,date_as_string1.split('-'))
         return day<= 31 and month<= 12 and year<= 3999

     @classmethod
     def pre_out(cls,date_string):
         if cls.is_date_valid(date_string):
             year, month, day= map(int,date_string.split("-"))
             return cls(year, month, day)
         else:
             return cls(1900,1,1)
 
 date= '2017-13-6'
 demo2= Demo.pre_out(date) 
 print(demo2.out_date())
 
 #  year:1900, month:1, day:1

比如,上文中的Student类中,我们想判断学生是否放寒暑假,就可以使用静态方法,它只是对日期做这个类相关的逻辑判断,并没有对类或实例属性做修改等

 class Student():
 
     def __init__(self,name,score):
         self.__name= name
         self.__score= score
 
     @staticmethod
     def is_holiday(day):
         num_month= day.month
             if num_monthin [7,8]:
                 print('Students are on holiday!')
             else:
                 print('Students are attending school.')
 
 from datetime import date
 
 day= date.today()
 Student.is_holiday(day)  # 不需要实例化类就可以被类本身调用
 
 #    Students are attending school.

参考链接:
https://www.jianshu.com/p/eea864224e2d
https://blog.csdn.net/dyh4201/article/details/78336529
https://blog.csdn.net/ljt735029684/article/details/80714274
https://www.zhihu.com/question/20021164/answer/18224953
https://www.cnblogs.com/wangyongsong/p/6750454.html
https://www.jianshu.com/p/b92be62d7ad7
https://blog.csdn.net/caroline_wendy/article/details/23383995

相关文章

  • Python类和对象笔记

    小白笔记仅记录常规操作中较为不熟悉的操作类型 1、类可以理解为一种自定义的数据类型,它定义了这一类对象普遍具有的或...

  • Python3.5笔记——第8章 面向对象编程

    Python3.5笔记 第8章 面向对象编程 面向对象术语介绍 类:用来描述具有相同属性和方法的对象的集合。类定义...

  • python语法入门五

    类和对象 类和对象 基础 继承:python为多继承 类、类对象和实例对象 当实例对象声明后,再删除类对象,那么该...

  • Python学习笔记——类和对象

    类和对象 1. 一个例子 2. 面向对象的特征 ① 封装 ② 继承 ③ 多态 3. self 4. _ _ in...

  • Python环境搭建

    Day2Python学习笔记+开发环境搭载 Python面向对象特性: 类的定义 类定义class关键字 多重继承...

  • Python3 & 类方法,实例方法,静态方法详解

    类对象和实例对象 类:Python中一切皆为对象,对象是抽象的,Python中的对象用类来表示。而在实示使用时,通...

  • Python学习笔记(12)-黑马教程-面向对象之封装

    前言 这是黑马Python教程的笔记,内容是有关面向对象。 什么是类 类是一种对象,每个对象都属于特定的类,并被称...

  • Python ☞ day 8

    Python学习笔记之 面向对象 第一个python类 设计类类名:见名之意,首字母大写,其他遵循驼峰原则属性:见...

  • python 面向对象编程之类和实例

    python学习笔记,特做记录,分享给大家,希望对大家有所帮助。 类和实例 面向对象最重要的概念就是类(Class...

  • python 类和对象

    一、面向对象编程 编程思想:1.面向过程编程 ---> 算法,逻辑(数学逻辑)2.函数式编程 ---> 函数,模...

网友评论

      本文标题:Python类和对象笔记

      本文链接:https://www.haomeiwen.com/subject/pzclbctx.html