- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Python中关于property使用的小技巧由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
一种用起来像是使用实例属性一样的特殊属性,可以对应于某个方法 。
既要保护类的封装特性,又要让开发者可以使用 对象.属性 的方式操作方法,@property 装饰器,可以直接通过方法名来访问方法,不需要在方法名后添加一对 () 小括号.
来看下求圆的面积的例子 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
class
Circle(
object
):
PI
=
3.14
def
__init__(
self
, r):
# r圆的半径
self
.r
=
r
self
.__area
=
self
.PI
*
self
.r
*
self
.r
@property
def
area(
self
):
return
self
.__area
def
get_area(
self
):
return
self
.__area
In [
2
]: c
=
Circle(
10
)
In [
3
]: c.area
Out[
3
]:
314.0
In [
4
]: c.get_area()
Out[
4
]:
314.0
|
property属性的定义和调用要注意一下几点:
@property
装饰器;并且仅有一个 self
参数()
1
2
|
实例方法:c.get_area()
property
装饰的方法:c.area
|
对于某商城中显示电脑主机的列表页面,每次请求不可能把数据库中的所有内容都显示到页面上,而是通过分页的功能局部显示,所以在向数据库中请求数据时就要显示的指定获取从第 m 条到第 n条的所有数据 这个分页的功能包括:
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
32
33
34
35
36
37
|
class
Pager(
object
):
def
__init__(
self
, current_page):
# 用户当前请求的页码(第一页、第二页...)
self
.current_page
=
current_page
# 每页默认显示10条数据
self
.per_items
=
10
@property
def
start(
self
):
val
=
(
self
.current_page
-
1
)
*
self
.per_items
return
val
@property
def
end(
self
):
val
=
self
.current_page
*
self
.per_items
return
val
# ipython测验
In [
2
]: p
=
Pager(
1
)
In [
3
]: p.start
# 就是起始值,即:m
Out[
3
]:
0
In [
4
]: p.end
# 就是结束值,即:n
Out[
4
]:
10
In [
5
]: p
=
Pager(
2
)
In [
6
]: p.start
Out[
6
]:
10
In [
7
]: p.end
Out[
7
]:
20
|
@property
property
对象的类属性 property()
在类的实例方法上应用 @property 装饰器 。
Python中的类有旧式类 和 新式类,新式类 的属性比 旧式类的属性丰富.
旧式类,具有一种 @property 装饰器 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class
Goods:
def
__init__(
self
, name):
self
.name
=
name
@property
def
price(
self
):
return
100
# ipython测验
In [
10
]: g
=
Goods(
'手表'
)
In [
11
]: g.price
Out[
11
]:
100
|
新式类,具有三种 @property 装饰器 。
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
|
class
Goods:
"""
python3中默认继承object类
以python2、3执行此程序的结果不同,因为只有在python3中才有@xxx.setter @xxx.deleter
"""
@property
def
price(
self
):
print
(
'@property'
)
@price
.setter
def
price(
self
, value):
print
(
'@price.setter'
)
@price
.deleter
def
price(
self
):
print
(
'@price.deleter'
)
# ipython测验
In [
13
]: g
=
Goods()
In [
14
]: g.price
@property
In [
15
]: g.price
=
100
@price
.setter
In [
16
]:
del
g.price
@price
.deleter
|
g.price
单独调用自动执行 @property
修饰的 price
方法,并获取方法的返回值g.price = 100
赋值自动执行 @price.setter
修饰的 price
方法,并将 100
赋值给方法的参数del g.price
删除自动执行 @price.deleter
修饰的 price
方法注意 。
@property
修饰的方法@property
、@方法名.setter
、@方法名.deleter
修饰的方法由于新式类中具有三种访问方式,我们可以根据它们几个属性的访问特点,分别将三个方法定义为对同一个属性:获取、修改、删除.
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# Goods类@property应用
class
Goods(
object
):
def
__init__(
self
, name, price):
# 原价
self
.original_price
=
price
# 折扣
self
.discount
=
0.8
@property
def
price(
self
):
# 实际价格 = 原价 * 折扣
new_price
=
self
.original_price
*
self
.discount
return
new_price
@price
.setter
def
price(
self
, value):
self
.original_price
=
value
@price
.deleter
def
price(
self
):
print
(
'删除商品原价'
)
del
self
.original_price
# ipython测验
In [
22
]: g
=
Goods(
'小米手机'
,
2000
)
In [
23
]: g.price
Out[
23
]:
1600.0
In [
24
]: g.price
=
3000
In [
25
]: g.price
Out[
25
]:
2400.0
In [
26
]:
del
g.price
删除商品原价
In [
27
]: g.price
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
AttributeError Traceback (most recent call last)
<ipython
-
input
-
27
-
38ee45b469f2
>
in
<module>
-
-
-
-
>
1
g.price
<ipython
-
input
-
18
-
d5ea66eb7ece>
in
price(
self
)
12
def
price(
self
):
13
# 实际价格 = 原价 * 折扣
-
-
-
>
14
new_price
=
self
.original_price
*
self
.discount
15
return
new_price
16
AttributeError:
'Goods'
object
has no attribute
'original_price'
|
创建值为 property 对象的类属性,当使用类属性的方式创建 property 属性时,旧式类 和 新式类无区别 。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
class
Foo:
def
get_bar(
self
):
return
'get_bar'
BAR
=
property
(get_bar)
# ipython 测验
In [
32
]: f
=
Foo()
In [
33
]: f.BAR
Out[
33
]:
'get_bar'
|
f.BAR 自动调用 get_bar() 方法,并获取方法的返回值 。
property() 中有个四个参数 。
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
32
33
34
35
36
37
|
class
Foo(
object
):
def
__init__(
self
, bar):
self
.bar
=
bar
def
get_bar(
self
):
print
(
'get_bar'
)
return
self
.bar
def
set_bar(
self
, value):
"""必须要有两个参数"""
print
(
'set bar '
+
value)
self
.bar
=
value
def
del_bar(
self
):
print
(
'del bar'
)
del
self
.bar
BAR
=
property
(get_bar, set_bar, del_bar,
"bar description..."
)
# ipython测验
In [
50
]: f
=
Foo(
'python'
)
In [
51
]: f.BAR
get_bar
Out[
51
]:
'python'
In [
52
]: f.BAR
=
'Java'
set
bar Java
In [
53
]: f.BAR
get_bar
Out[
53
]:
'Java'
In [
54
]:
del
f.BAR
del
bar
|
由于 类属性方式 创建 property 对象属性具有3种访问方式,我们可以根据它们几个属性的访问特点,分别将三个方法定义为对 同一个属性:获取、修改、删除 ,跟 @property 装饰器对比.
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
32
33
34
35
36
37
38
39
|
# Goods类 property对象类属性 应用
class
Goods(
object
):
def
__init__(
self
, name, price):
# 原价
self
.original_price
=
price
# 折扣
self
.discount
=
0.8
def
get_price(
self
):
# 实际价格 = 原价 * 折扣
new_price
=
self
.original_price
*
self
.discount
return
new_price
def
set_price(
self
, value):
self
.original_price
=
value
def
del_price(
self
):
print
(
'删除商品原价'
)
del
self
.original_price
PRICE
=
property
(get_price, set_price, del_price,
"price description"
)
# ipython测验
In [
59
]: g
=
Goods(
'Mac电脑'
,
9000
)
In [
60
]: g.PRICE
Out[
60
]:
7200.0
In [
61
]: g.PRICE
=
10000
In [
62
]: g.PRICE
Out[
62
]:
8000.0
In [
63
]:
del
g.PRICE
删除商品原价
|
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
32
33
34
35
36
37
38
39
40
|
# Goods类 @property装饰器 应用
class
Goods(
object
):
def
__init__(
self
, name, price):
# 原价
self
.original_price
=
price
# 折扣
self
.discount
=
0.8
@property
def
price(
self
):
# 实际价格 = 原价 * 折扣
new_price
=
self
.original_price
*
self
.discount
return
new_price
@price
.setter
def
price(
self
, value):
self
.original_price
=
value
@price
.deleter
def
price(
self
):
print
(
'删除商品原价'
)
del
self
.original_price
# ipython测验
In [
59
]: g
=
Goods(
'Mac电脑'
,
9000
)
In [
60
]: g.PRICE
Out[
60
]:
7200.0
In [
61
]: g.PRICE
=
10000
In [
62
]: g.PRICE
Out[
62
]:
8000.0
In [
63
]:
del
g.PRICE
删除商品原价
|
可以发现两种都可以实现但 @property 装饰器的在 旧式类中只有 @property , 没有@method.setter 和@method.deleter,新式类则两种都可以使用。因此看大家的习惯,选一种.
大自然用数百亿年创造出我们现实世界,而程序员用几百年创造出一个完全不同的虚拟世界。我们用键盘敲出一砖一瓦,用大脑构建一切。人们把1000视为权威,我们反其道行之,捍卫1024的地位。我们不是键盘侠,我们只是平凡世界中不凡的缔造者 .
到此这篇关于Python中关于property使用的小技巧的文章就介绍到这了,更多相关Python property 内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。
原文链接:https://blog.csdn.net/qq_43629857/article/details/115322753 。
最后此篇关于Python中关于property使用的小技巧的文章就讲到这里了,如果你想了解更多关于Python中关于property使用的小技巧的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我是新手。查看 Google 新闻...上下滚动页面时请注意左侧导航栏。 看看它是如何滚动一点,然后在它消失之前粘在页面顶部的? 关于如何做到这一点有什么想法吗? jQuery 和 CSS 可以复制吗
技巧 1:在 Web 服务器上缓存常用数据 技巧 2:在 Application 或 Session 对象中缓存常用数据 技巧 3:在 Web 服务器磁盘上缓存数据和 HTML 技巧 4:避免
我在 excel 中有一个电子表格,其中包含以下行: COLUMN Value1.Value2.Value3 Value4.Value5.Value6 Value7.Value8.Val
GNU Makefile 中是否有任何技巧来获取规则的所有依赖项? 例子: rule1: dep1_1 dep1_2 dep1_3 rule2: dep2_1 dep2_2 rule1 dump_
人们使用什么来追踪内存泄漏?我已经通过代码检查设法解决了一些问题,但我不知道下一步该做什么/当我的程序变大时我将如何管理问题。我知道我在泄漏什么类型的对象,但我不知道是什么让它保持活力。 在 Wind
有什么好的方法可以将“xlSum”、“xlAverage”和“xlCount”等字符串转换为它们在 Microsoft.Office.Interop.Excel.XlConsolidationFunc
我们都见过这个: javascript:document.body.contentEditable='true'; document.designMode='on';无效 0 但我的问题是,这实际上是
我的应用程序将输出一个图形,其布局由用户定义。自定义布局类应该实现我定义的接口(interface)。我应该怎么做?有一个特殊的文件夹,我可以在其中查找布局类?用户是否将类名作为参数传递给应用? 如有
我在弄清楚如何在 Javascript 中自引用表行时遇到了一些麻烦。 这是简化的代码: $( "#listitems tbody" ).append( "" + "" + id.va
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 6 年前。 Improve this q
我正在将代码库从一种编程风格转移到另一种编程风格。 我们有一个名为 Operand 的类型,定义如下: class Operand {...}; 然后我们有 class OperandFactory
我使用以下缩略图类在我的内容包装器中显示 4x3 缩略图: .thumbnail { float:left; width:300px; height:200px; ma
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我认为这是不可能的,但我想在放弃之前问问你。 我想要类似 constexpr 增量的东西。 #include constexpr int inc() { static int inc = 0;
是否有任何适合 C++ 新手的技术和描述的好列表。我在想一个描述 RAII、RVO、左值的列表……这适用于目前不了解这些技术或来自不适用这些技术的其他语言的新手。 最好是短小精悍的:-) 最佳答案 是
我有一个二进制字符串 '01110000',我想在不编写 forloop 的情况下返回前面的前导零数。有谁知道如何做到这一点?如果字符串立即以“1”开头,最好也返回 0 最佳答案 如果您真的确定它是一
我需要优化我的应用程序的 RAM 使用率。 请省去那些告诉我在编写 Python 代码时不应该关心内存的讲座。我有内存问题,因为我使用非常大的默认字典(是的,我也想快点)。我目前的内存消耗是 350M
有时,当我看到一个我喜欢的网站或来自受人尊敬的人的网站时,我会查看源代码并尝试理解它们(就像我们所有人一样)。 关于 Jeremy Keiths他使用以下代码的网站: [role="navigatio
这是我怎样设置 Git 来管理我的家目录的方法。 我有好几台电脑。一台笔记本电脑用于工作,一台工作站放在家里,一台树莓派(或四台),一台 Pocket CHIP,一台 运行
shell 技巧 表变量 HBase 0.95 版本增加了为表提供 jruby 风格的面向对象引用的 shell 命令。以前,作用于表的所有 shell 命令都具有程序风格,该风格始终将表的名称作
我是一名优秀的程序员,十分优秀!