部分遗留Python语法(0)

以下大部分均搬运自:Python's @classmethod and @staticmethod Explained

@classmethod & @staticmethod

class Student(object):

    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name

    @classmethod
    def from_string(cls, name_str):
        first_name, last_name = map(str, name_str.split(' '))
        student = cls(first_name, last_name)
        return student


if __name__ == "__main__":
    scott = Student('Scott',  'Robinson')

    scott1 = Student.from_string('Scott Robinson')

其中在@classmethod中cls是作为class methods的the first argument,就像是实例化方法__init__中的self作为instance methods的the first argument。(参考PEP 8

和@staticmethod的一个优点是,不要实例化就可以调用(类)方法。但是@staticmethod是在不传cls进去的情况下调用,例如:

class Student(object):

    @staticmethod
    def is_full_name(name_str):
        names = name_str.split(' ')
        return len(names) > 1

Student.is_full_name('Scott Robinson')   # True
Student.is_full_name('Scott')            # False

相同点:都是decorator(A Python decorator is a specific change to the Python syntax that allows us to more conveniently alter functions and methods (and possibly classes in a future version). );These types of methods aren't typically meant to create/instantiate objects, but they may contain some type of logic pertaining to the class itself, like a helper or utility method.

主要区别:This cls parameter is the class object we talked about, which allows @classmethod methods to easily instantiate the class, regardless of any inheritance going on. The lack of this cls parameter in @staticmethod methods make them true static methods in the traditional sense. 

讲到底是@classmethod是一种可以轻松完成实例化的方法,而@staticmethod是真正的“静态”方法。

文章最后还留个例子:

class ClassGrades:

    def __init__(self, grades):
        self.grades = grades

    @classmethod
    def from_csv(cls, grade_csv_str):
        grades = map(int, grade_csv_str.split(', '))
        cls.validate(grades)
        return cls(grades)


    @staticmethod
    def validate(grades):
        for g in grades:
            if g < 0 or g > 100:
                raise Exception()

try:
    # Try out some valid grades
    class_grades_valid = ClassGrades.from_csv('90, 80, 85, 94, 70')
    print 'Got grades:', class_grades_valid.grades

    # Should fail with invalid grades
    class_grades_invalid = ClassGrades.from_csv('92, -15, 99, 101, 77, 65, 100')
    print class_grades_invalid.grades
except:
    print 'Invalid!'

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python遗留物检测项目是指通过对Python代码进行扫描和分析,检测其中可能存在的过期、废弃或不再使用的代码、库或语法。在长期的软件开发过程中,随着需求的变化和技术的不断更新,原本有效的代码可能变得过时或无效,这些遗留物通常会增加维护成本、降低代码质量和开发效率。 Python遗留物检测项目的目标是及时发现和解决这些问题,使代码保持高质量和可维护性。该项目可以通过静态代码分析工具、代码审查和自动化测试等方式来实现。 具体来说,该项目可以包括以下方面的内容: 1. 检测废弃的库或模块:通过扫描代码中引用的外部库和模块,检测是否有已经废弃的版本或者已经有更好的替代品。及时更新废弃的库可以提高代码的安全性和性能。 2. 检测过时的函数和语法:检查代码中使用的Python函数和语法是否已经过时,是否有更好的替代选择。及时更新过时的函数和语法可以减少代码中的陈旧和冗余部分。 3. 检测无效的代码块:通过对代码进行语法分析,检测其中存在的无效、冗余或不必要的代码块,减少代码的复杂性和可读性。 4. 检测未使用的变量和函数:检查代码中定义但未被使用的变量和函数,避免占用内存和增加代码的复杂性。 5. 自动化测试和代码覆盖率:通过编写自动化测试用例,并评估代码的覆盖率,发现未正确覆盖的代码区域,提高代码的可靠性和稳定性。 综上所述,Python遗留物检测项目是一个重要的质量保障项目,可以帮助开发团队发现和解决代码中的问题,提高代码质量和可维护性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值