乔克视界 乔克视界
首页
  • 运维
  • 开发
  • 监控
  • 安全
  • 随笔
  • Docker
  • Golang
  • Python
  • AIOps
  • DevOps
  • 心情杂货
  • 读书笔记
  • 面试
  • 实用技巧
  • 博客搭建
友链
关于
收藏
  • 分类
  • 标签
  • 归档

乔克

云原生爱好者
首页
  • 运维
  • 开发
  • 监控
  • 安全
  • 随笔
  • Docker
  • Golang
  • Python
  • AIOps
  • DevOps
  • 心情杂货
  • 读书笔记
  • 面试
  • 实用技巧
  • 博客搭建
友链
关于
收藏
  • 分类
  • 标签
  • 归档
  • Docker

  • Golang

  • AIOps

  • Python

    • 基础知识

      • Python之链表
      • Python 之类的初识
        • 一、类的初识
        • 二、类的字段
        • 三、类的方法
      • Python之函数式编程
      • Python之匿名函数
      • Python之自定义函数
      • Python之异常处理
      • Python之条件与循环
      • Python之列表生成式
      • Python之生成器
      • Python之装饰器
      • Python之迭代器
      • Python之进程、线程、协程
      • Python之深浅拷贝
      • Python之反射
      • Python之并发编程
      • Python之垃圾回收机制
      • Python之断言assert
      • Python之上下文管理器和with语句
      • Python中JSON的对应关系
      • Python之单例模式
    • Django框架

    • 其他

  • DevOps

  • 专栏
  • Python
  • 基础知识
乔克
2025-07-19
目录

Python 之类的初识

# 一、类的初识

类,是一群有着相同属性和方法的对象的集合。

我们先来看下面一个例子:

>>> class Foo:
...     def __init__(self, name, age):
...         self.name = name
...         self.age = age
...         self.__money = 100
...     def get_name(self):
...         return self.name
...     def get_money(self):
...         return self.__money
...
>>> f = Foo("joker",18)
>>> print(f.get_name)
<bound method Foo.get_name of <__main__.Foo object at 0x000001F4B8339438>>
>>> print(f.get_name())
joker
>>> print(f.get_money())
100
>>> print(f.name)
joker
>>> print(f.age)
18
>>> print(f.__money)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Foo' object has no attribute '__money'
>>>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

结合上面的例子,做几个名称解释:

(1)、类:一群有着相似性事物的集合,对应上面的 class(关键字),Foo 是类名

(2)、对象:集合中的事物,由类生成,对应上面的 f

(3)、属性:对象的某个特征,对应上面的 name,age,__money

(4)、方法:对象的某个动态能力,对应上面的 get_name(),get_money()

从上面可以看到 class 定义了一个类 Foo,它的下面有三个方法,其中 init()是构造方法,它会在生成对象的时候自动执行,而 get_name 和 get_money 是普通方法,由对象来调用。

Foo 类有三个属性值(name,age,money),其中 name 和 age 是普通属性值,而money 是私有属性值,普通属性值可以使用对象调用得到具体的值,而私有的属性值只能在类中调用,对象无法调用,如果需要获得私有属性值的内容,可以像上面定义一个方法,通过对象调用方法取得私有属性。

# 二、类的字段

类中的字段包含静态字段和普通字段。其中静态字段保存在类中,可以通过对象和类访问,普通字段保存在对象中,只能通过对象访问。

如下例子:

>>> class Foo:
...     NAME = "中国"
...     def __init__(self, city):
...         self.city = city
...
>>> f = Foo("上海")
>>> print(f.NAME)
中国
>>> print(Foo.NAME)
中国
>>> print(f.city)
上海
>>> print(Foo.city)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'Foo' has no attribute 'city'
>>>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

上面例子中 NAME 是静态字段,city 是普通字段,静态字段 NAME 可以通过对象 f 和类 Foo 调用(如上),而普通字段只能通过对象 f 调用。

# 三、类的方法

类得方法分为:

(1)、普通方法

(2)、静态方法

(3)、类方法

应用场景如下:

(1)、如果对象中需要保存一些值,调用方法需要调用对象中的值,用普通方法;

(2)、如果不需要对象中的值,用静态方法;

(3)、在方法中如果需要当前的类名,用类方法;

>>> class Foo:
...     def bar(self):
...         print("bar")
...     @staticmethod
...     def static_method():
...         print("static method")
...     @staticmethod
...     def static_method2(a,b):
...         print(a,b)
...     @classmethod
...     def class_method(cls):
...         print("class method",cls)
...
>>> f = Foo()
>>> f.bar()
bar
>>> Foo.bar(f)
bar
>>> f.static_method()
static method
>>> Foo.static_method()
static method
>>> Foo.static_method(1,2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: static_method() takes 0 positional arguments but 2 were given
>>> Foo.static_method2(1,2)
1 2
>>> Foo.class_method()
class method <class '__main__.Foo'>
>>>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

上面定义了一个类 Foo,它里面有普通方法 bar(),有静态方法 static_method(),static_method2(),还有类方法 class_method(),其中@staticmethod 表示该方法是静态方法,@classmethod 表示该方法是类方法。它们都可以通过对象和类名调用,其中,如果用类名调用普通方法,是要传对象名的,调用静态方法,不用传参数,调用类方法也不用传参数。

作者:乔克

本文链接:https://jokerbai.com

版权声明:本博客所有文章除特别声明外,均采用 署名-非商业性-相同方式共享 4.0 国际 (CC-BY-NC-SA-4.0) 许可协议。转载请注明出处!

上次更新: 2025/07/19, 11:33:23
Python之链表
Python之函数式编程

← Python之链表 Python之函数式编程→

最近更新
01
使用 Generic Webhook Trigger 触发 Jenkins 多分支流水线自动化构建
07-19
02
使用Zadig从0到1实现持续交付平台
07-19
03
基于Jira的运维发布平台
07-19
更多文章>
Theme by Vdoing | Copyright © 2019-2025 乔克 | MIT License | 渝ICP备20002153号 |
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式