本文共 5753 字,大约阅读时间需要 19 分钟。
手机怎么安装py thon
Hello friends, today we will learn about Python property decorator. In our previous tutorial, we have discussed , you should read that if you are not familiar with the decorator before learning about python property decorator.
朋友您好,今天我们将学习Python属性装饰器。 在上一教程中,我们讨论了 ,如果您不了解 ,则应该阅读以下内容,然后再学习python属性装饰器。
As we discussed that we can decorate a function using decorators. There are some built-in decorators.
正如我们所讨论的,我们可以使用装饰器装饰一个函数。 有一些内置的装饰器。
Python @property is one of the built-in decorators. The main purpose of any decorator is to change your class methods or attributes in such a way so that the user of your class no need to make any change in their code.
Python @property是内置装饰器之一。 任何装饰器的主要目的都是以这种方式更改类的方法或属性,以使类的用户无需对其代码进行任何更改。
Consider the following class segment:
考虑以下课程段:
class Student: def __init__(self, name, marks): self.name = name self.marks = marks self.gotmarks = self.name + ' obtained ' + self.marks + ' marks'st = Student("Jaki", "25")print(st.name)print(st.marks)print(st.gotmarks)
It will output as below:
它将输出如下:
Jaki25Jaki obtained 25 marks
Now say we want to change the name attribute of the student class then what will happen? Append the following 3 line after the previous code:
现在说我们要更改学生班级的名称属性,那么会发生什么? 在前面的代码后添加以下3行:
st.name = "Anusha"print(st.name)print(st.gotmarks)
Then the output will be:
然后输出将是:
Notice that the name attribute got changed but the sentence that was created by the gotmarks
attribute remained same as it was set during the initialization of the student object.
注意,名称属性已更改,但是由gotmarks
属性创建的句子与在学生对象初始化期间设置的句子相同。
But we want gotmarks also to be changed when student name is updated. Here comes the use of python property decorator.
但是我们希望在学生姓名更新时也可以更改标记。 这是python属性装饰器的使用。
We can solve this problem with the following code.
我们可以使用以下代码解决此问题。
class Student: def __init__(self, name, marks): self.name = name self.marks = marks # self.gotmarks = self.name + ' obtained ' + self.marks + ' marks' def gotmarks(self): return self.name + ' obtained ' + self.marks + ' marks'st = Student("Jaki", "25")print(st.name)print(st.marks)print(st.gotmarks())st.name = "Anusha"print(st.name)print(st.gotmarks())
It will output as follow:
它将输出如下:
Jaki25Jaki obtained 25 marksAnushaAnusha obtained 25 marks
Wow!!! Our requirement is solved. But have a close look at the code. We have removed the gotmarks attribute from the constructor and added a method named gotmarks.
哇!!! 我们的要求已解决。 但是请仔细看一下代码。 我们从构造函数中删除了gotmarks属性,并添加了一个名为gotmarks的方法。
So now in our class there is no attribute named gotmarks
and we have a method named gotmarks()
.
因此,在我们的类中,现在没有名为gotmarks
属性,而我们有一个名为gotmarks()
的方法。
And for this change, any user who is using my class will be in trouble as they have to replace all the attribute gotmarks with a function call gotmarks(). Assume there are 1000 lines of code then how troublesome it will be for the coder.
对于此更改,使用我的类的任何用户都将遇到麻烦,因为他们必须用函数调用gotmarks()替换所有属性gotmarks。 假设有1000行代码,那么对编码器来说将是多么麻烦。
So we will solve this problem in pythonic way using the python property decorator. Notice the following code:
因此,我们将使用python属性装饰器以pythonic方式解决此问题。 请注意以下代码:
@propertydef gotmarks(self): return self.name + ' obtained ' + self.marks + ' marks'
It will provide the same output as before, and don’t forget to remove the ‘()’ after gotmarks when printing it. Just writing @property above the function gotmarks() make it available to be used as a property.
它会提供与以前相同的输出,并且在打印时,请不要忘记在获得标记后删除'()'。 只需在功能gotmarks()上方编写@property,即可将其用作属性。
And the user of our class doesn’t even know that attribute gotmarks is removed and we have a function for that. This is how the property decorator helps in keeping our code loosely coupled with the client code.
而且我们班的用户甚至都不知道属性标记被删除了,为此我们有一个函数。 这就是属性装饰器如何使我们的代码与客户端代码保持松散耦合的方式。
Now let’s say we want to set the name and marks attribute when we change the value of gotmarks. Deeply observe the code:
现在假设我们要在更改gotmarks的值时设置name和marks属性。 深入观察代码:
class Student: def __init__(self, name, marks): self.name = name self.marks = marks # self.gotmarks = self.name + ' obtained ' + self.marks + ' marks' @property def gotmarks(self): return self.name + ' obtained ' + self.marks + ' marks' @gotmarks.setter def gotmarks(self, sentence): name, rand, marks = sentence.split(' ') self.name = name self.marks = marksst = Student("Jaki", "25")print(st.name)print(st.marks)print(st.gotmarks)print("##################")st.name = "Anusha"print(st.name)print(st.gotmarks)print("##################")st.gotmarks = 'Golam obtained 36'print(st.gotmarks)print(st.name)print(st.marks)
As we want to update the value of name and marks when we are setting the value of gotmarks. So using the setter
of @proprety decorator we can achieve this.
因为我们要在设置gotmarks的值时更新name和mark的值。 因此,使用@proprety装饰器的setter
可以实现此目的。
Notice that we have written @gotmarks.setter
that means we are applying the setter on the gotmarks method. And then we are splitting the sentence and updating the value of name and marks.
请注意,我们已经编写了@gotmarks.setter
,这意味着我们将setter应用于gotmarks方法。 然后我们拆分句子并更新名称和标记的值。
The above python property decorator with setter will produce below output.
上面的带有setter的python属性装饰器将产生以下输出。
Jaki25Jaki obtained 25 marks##################AnushaAnusha obtained 25 marks##################Golam obtained 36 marksGolam36
That’s all about python property decorator and python @property setter examples.
这就是关于python属性装饰器和python @property setter示例的全部内容。
翻译自:
手机怎么安装py thon
转载地址:http://gelzd.baihongyu.com/