Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何使用decorator和property检查类属性';有效的类型和范围? 导入数学 随机输入 #==问题1 类矩形房间(对象): """ 矩形房间表示一个矩形区域,其中包含干净或脏的房间 瓷砖。 房间有一个宽度和一个高度,并包含(宽度*高度)瓷_Python 3.x_Decorator - Fatal编程技术网

Python 3.x 如何使用decorator和property检查类属性';有效的类型和范围? 导入数学 随机输入 #==问题1 类矩形房间(对象): """ 矩形房间表示一个矩形区域,其中包含干净或脏的房间 瓷砖。 房间有一个宽度和一个高度,并包含(宽度*高度)瓷

Python 3.x 如何使用decorator和property检查类属性';有效的类型和范围? 导入数学 随机输入 #==问题1 类矩形房间(对象): """ 矩形房间表示一个矩形区域,其中包含干净或脏的房间 瓷砖。 房间有一个宽度和一个高度,并包含(宽度*高度)瓷,python-3.x,decorator,Python 3.x,Decorator,如何使用decorator和property检查类属性';有效的类型和范围? 导入数学 随机输入 #==问题1 类矩形房间(对象): """ 矩形房间表示一个矩形区域,其中包含干净或脏的房间 瓷砖。 房间有一个宽度和一个高度,并包含(宽度*高度)瓷砖。每个瓷砖 有一定量的污垢。只有当污垢量 这块瓷砖上的污垢厚度为0。 """ 定义初始值(自身、宽度、高度、污垢量): """ 初始化具有指定宽度、高度和宽度的矩形房间 每块瓷砖上的污垢量。 宽度:一个大于0的整数 高度:一个大于0的整数 污

如何使用decorator和property检查类属性';有效的类型和范围?
导入数学
随机输入
#==问题1
类矩形房间(对象):
"""
矩形房间表示一个矩形区域,其中包含干净或脏的房间
瓷砖。
房间有一个宽度和一个高度,并包含(宽度*高度)瓷砖。每个瓷砖
有一定量的污垢。只有当污垢量
这块瓷砖上的污垢厚度为0。
"""
定义初始值(自身、宽度、高度、污垢量):
"""
初始化具有指定宽度、高度和宽度的矩形房间
每块瓷砖上的污垢量。
宽度:一个大于0的整数
高度:一个大于0的整数
污垢量:整数>=0
"""
self.width,self.height,self.dirty\u amount=宽度,高度,dirty\u amount
瓷砖=[(宽,高)表示范围内的宽(宽)表示范围内的高(高)]
self.room={tile:tile for tile in tiles for dirty in[dirty_amount]}
#引发未实现的错误
def get_宽度(自身):
返回自我宽度
def设置_宽度(自身,值):
如果值<代码>导入类型
从functools导入包装
def范围检查(func):
代码=函数。\代码__
allargs=list(code.co\u varnames[:code.co\u argcount])
@包裹
def_装饰器(*args,**kargs):
位置=allargs[:len(args)]
对于argname,请签入func.\u注释\u.items():
如果argname在kargs中:
如果没有检查(卡格斯[argname]):
raise TypeError(“%s”检查失败“%argname”)
位置中的elif argname:
pos=位置索引(argname)
如果未选中(参数[pos]):
raise TypeError(“%s”检查失败“%argname”)
其他:
#默认参数
通过
返回函数(*args,**kargs)
返回装饰器
def范围(比率):

return 0 Wait@crystalizeee,这是不是格式化错误,或者你没有缩进类下面的函数?天哪,是的,当我从IDE复制粘贴它时,不知怎的格式化错误了。你能忽略这个愚蠢的错误吗?谢谢!
import math
import random

# === Problem 1

class RectangularRoom(object):
"""
A RectangularRoom represents a rectangular region containing clean or dirty
tiles.

A room has a width and a height and contains (width * height) tiles. Each tile
has some fixed amount of dirt. The tile is considered clean only when the amount
of dirt on this tile is 0.
"""

    def __init__(self, width, height, dirt_amount):
        """
        Initializes a rectangular room with the specified width, height, and 
        dirt_amount on each tile.

        width: an integer > 0
        height: an integer > 0
        dirt_amount: an integer >= 0
        """

        self.width, self.height, self.dirt_amount = width, height, dirt_amount
        tiles = [(w,h) for w in range(width) for h in range(height)]
        self.room = {tile:dirt for tile in tiles for dirt in [dirt_amount]}
        #raise NotImplementedError

    def get_width(self):

        return self._width

    def set_width(self, value):
        if value <= 0 :
            raise ValueError("Must be greater than 0")
        self._width = value

     width = property(get_width,set_width)

    def __str__(self):
        return str((self.room))
import types
from functools import wraps


def range_check(func):
    code = func.__code__
    allargs = list(code.co_varnames[:code.co_argcount])
    @wraps
    def _decorator(*args, **kargs):
        positionals = allargs[:len(args)]
        for argname, check in func.__annotations__.items():
            if argname in kargs:
                if not check(kargs[argname]):
                    raise TypeError('"%s" check failed' % argname)
            elif argname in positionals:
                pos = positionals.index(argname)
                if not check(args[pos]):
                    raise TypeError('"%s" check failed' % argname)
            else:
                # argument defaulted
                pass
        return func(*args, **kargs)
    return _decorator

def range(ratio):
    return 0 <= ratio and ratio <= 0.5

class Employee:
    def __init__(self, name, age, pay):
        self.name = name
        self.age = age
        self.pay = pay
    @range_check
    def giveRaise(self, ratio: range):
        self.pay *= (1 + ratio)

if __name__ == '__main__':
    alice = Employee('alice', 20, 50000)
    alice.giveRaise(0.1)
    alice.giveRaise(0.6)