python--把一个方法变成属性调用

# coding=utf-8

‘‘‘
装饰器(decorator)可以给函数动态加上功能,对于类的方法,装饰器一样起作用。Python内置的@property装饰器就是负责把一个方法变成属性调用的:
@property:把一个getter方法变成属性
@score.setter:负责把一个setter方法变成属性赋值
‘‘‘
class Screen(object):

    #读属性
    @property
    def width(self):
        return self.value_of_width

    #写属性
    @width.setter
    def width(self, value):
        self.value_of_width = value

    @property
    def height(self):
        return self.value_of_height

    @height.setter
    def height(self, value):
        self.value_of_height = value

    @property
    def resolution(self):
        return self.value_of_height*self.value_of_width

if __name__ == ‘__main__‘:
    s = Screen()
    s.width = 1024
    s.height = 768
    print(s.resolution)
    assert s.resolution == 786432, ‘1024 * 768 = %d ?‘ % s.resolution
时间: 2024-08-05 00:30:00

python--把一个方法变成属性调用的相关文章

python之特殊方法、属性和迭代器

9.1 准备工作 class NewStyle(object) more_code_here class OldStyle: more_code_here 在这两个类中,NewStyle是新式的类,OldStyle是旧式的类.如果文件以__metaclass__=type开始,那么两个类都是新式类. 除此之外,还可以在自己的类的作用域中对__metaclass__变量赋值.这样只会为这个类设定元类.元类是其他类的类-----这是个更高级的主题. 9.2 构造方法 当一个对象被创建后,会立即调用构

多重继承方法或属性调用顺序(MRO)

参考摘选自这篇文章http://hanjianwei.com/2013/07/25/python-mro/ python2.3以后采用c3方法来确定方法解析顺序 我们把类 C 的线性化(MRO)记为 L[C] = [C1, C2,-,CN].其中 C1 称为 L[C] 的头,其余元素 [C2,-,CN] 称为尾.如果一个类 C 继承自基类 B1.B2.--.BN,那么我们可以根据以下两步计算出 L[C]: L[object] = [object] L[C(B1-BN)] = [C] + merg

python中函数和方法区别,以及如何给python类动态绑定方法和属性(涉及types.MethodType()和__slots__)

网上有很多同义但不同方式的说法,下面的这个说法比较让你容易理解和接受 1 与类和实例无绑定关系的function都属于函数(function): 2 与类和实例有绑定关系的function都属于方法(method). “与类和实例无绑定关系”就道出了其中的关键 我们知道python是动态的编程语言,python的类除了可以预先定义好外,还可以在执行过程中,动态地将函数绑定到类上,绑定成功后,那些函数就变成类的方法了. 定义User类 可以使用__slots__来限制绑定的属性和方法 1 user

Spring MVC一个方法适用多种调用方式

web.xml spring-mvc.xml <mvc:annotation-driven /> <context:component-scan base-package="com.cnfwsy" /> <context:annotation-config /> <!-- 避免IE在ajax请求时,返回json出现下载 <bean id="jacksonMessageConverter" class="org

Python面向对象高级编程:@property--把方法变为属性

为了限制score的范围,可以通过一个set_score()方法来设置成绩,再通过一个get_score()来获取成绩,这样,在set_score()方法里,就可以检查参数: 1 >>> class Student(object): 2 def get_score(self): 3 return self.__score 4 def set_score(self,value): 5 if not isinstance(value,int): 6 raise ValueError('sec

python staticmethod,classmethod方法的使用和区别以及property装饰器的作用

class Kls(object): def __init__(self, data): self.data = data def printd(self): print(self.data) @staticmethod def smethod(*arg): print('Static:', arg) @classmethod def cmethod(*arg): print('Class:', arg) >>> ik = Kls(23) >>> ik.printd()

设计计算机类要求如下:属性:品牌、颜色、cpu型号,内存容量,硬盘大小,价格,工作状态;方法:打开,关闭,休眠;创建一个计算机对象,调用打开、关闭方法

代码如下: 1 //作者:realjanushu 2 //日期:17/9/28 3 /*功能: 4 设计计算机类要求如下: 5 6 属性:品牌.颜色.cpu型号,内存容量,硬盘大小,价格,工作状态: 7 8 方法:打开,关闭,休眠: 9 10 创建一个计算机对象,调用打开.关闭方法 11 */ 12 public class ComputerDemo{ 13 public static void main(String[] args){ 14 Computer c1 = new Computer

Python——property(使一个方法看起来就像类属性一样)

""" 装饰器property:     使一个方法看起来就像类属性一样 """ #例子1 class A:     def __init__(self,  x, y):         self.__x = x #私有变量         self.__y = y def __add(self): #私有方法         return self.__x + self.__y @property     def sum(self): #通过p

Python基础教程(第九章 魔法方法、属性和迭代器)

本文内容全部出自<Python基础教程>第二版,在此分享自己的学习之路. ______欢迎转载:http://www.cnblogs.com/Marlowes/p/5437223.html______ Created on Marlowes 在Python中,有的名称会在前面和后面都加上两个下划线,这种写法很特别.前面几章中已经出现过一些这样的名称(如__future__),这种拼写表示名字有特殊含义,所以绝不要在自己的程序中使用这样的名字.在Python中,由这些名字组成的集合所包含的方法称