首页 > 编程语言 >Python - class Method and static Method

Python - class Method and static Method

时间:2024-06-17 22:36:16浏览次数:26  
标签:classmethod Python object argument Method method static class

The methods in a class are associated with the objects created for it. For invoking the methods defined inside the method, the presence of an instance is necessary. The classmethod is a method that is also defined inside the class but does not need any object to invoke it. A classmethod can be invoked using the name of the class. For making a method to a class method, the function decorator "classmethod" receives the class as the implicit first argument using "cls" argument. This is similar to the methods receiving object as the first argument using the "self" argument. The syntax for using classmethod is shown below.

As shown in the above syntax, any method can be made into classmethod by adding "@classmethod" decorator before the definition of the method. Classmethod is bound to class but not to objects.

In the above code, the constructor consists of assigning values for attributes "name" and "age". The function to convert year to age is defined and converted to classmethod using "@classmethod". The cls() function in the return statement of the class method from_birth_year invokes the constructor of the class Person by creating the object. Here, the object name is taken care by the interpreter and the created object is returned. The statement "cls(name, date.today().year-year)" creates an object for the class (which is received as the first argument of the classmethod), and its first argument corresponds to the name and the second attribute finds the difference between the current year and the year which is passed on to the method. The class method returns a cls() method, which is an object of the class.

Now an object is created for the class Person and called with two parameters, "Mayank" and 21. The instance variables name and age are assigned with the values, respectively.

The above code can also be done using the classmethod from_birth_year as follows.

 

Static Method

Static method is similar to a class method, which can be accessed without an object. A static method is accessible to every object of a class, but methods defined in an instance are only able to be accessed by that object of a class. Static methods are not allowed to access the state of the objects (attributes of the class). The syntax for the static method is as follows.

In the above syntax, the method is converted to static method by having "@staticmethod" before the method definition. Also, "self" is not associated with the argument list of the static method.

In the above code, the "is_adult()" method is defined and converted to a static method that returns true, if the given argument is greater than 18 or returns false. Note that the function "is_adult()" does not have any "self" argument, and also, the function is not defined in such a way that it alters the state of the object.

In the above method, the created object with 'Mayank' and 21 shows 'True' on calling with the method is_adult. Unlike the above, the method is_adult can be invoked without creating an object.

 

The differences between classmethod and staticmethod are shown below.

 

标签:classmethod,Python,object,argument,Method,method,static,class
From: https://www.cnblogs.com/zhangzhihui/p/18253355

相关文章

  • 精准控制:Python 输入数值范围限制详解
    前言在实际开发过程中,经常需要对用户输入的数值进行限制,以确保输入的数据在合理的范围内。这不仅能防止程序错误,还能提高用户体验。作为一名测试工程师,掌握如何在Python中限制输入数值范围是非常有用的技能。本文将详细介绍如何使用Python实现这一功能,包括基础方法和高级应用......
  • Python - Overloading
    ObjectOrientedProgramming(OOP)allowstheprogrammerstoaddsomeadditionalfunctionalitiestotheoperatorsandmethodswhichhavebasicproperties.Suchakindofredefiningoftheentitiesoftheprogrammingstructureiscalledaspolymorphism.In......
  • Python函数式编程
    函数式编程基础函数式编程与面向对象编程一样都是一种编程范式,函数式编程也称为面向函数的编程。Python提供了高阶函数、函数类型和lambda表达式,他们是实现函数式编程的基础。高阶函数与函数类型如果一个函数可以作为其他函数的参数或者其他函数的返回值,那么这个函数就是“......
  • 使用python脚本玩转古早TCAD软件(待更新)
    前言TCAD(TechnologyComputerAidedDesign),虽然原名中没有与半导体器件有关的词汇,但这种软件便是半导体工艺模拟及器件模拟的工具,可以说是EDA软件的一种。TCAD软件同其他EDA软件一样,底层需要复杂的数学模型和数物模型支撑,能大幅减少半导体制造的研发成本,为新型半导体器件提供初......
  • Python - pandas 利用 某一列的值过滤数据
    #FA存在3D不存在建模的代码(1).txtEDLG-S1-M3-L12有一个excel:需求:利用txt中的代码去匹配execl中的调整后的规格型号,将匹配的数据保留,生成新的excelimportpandaswithopen('FA存在3D不存在建模的代码(1).txt','r')asf:txt_codes={item.replace('\n','')......
  • [AIGC] Python内置函数:刷题必备
    在Python编程和刷题过程中,我们经常会使用到一些内置函数来提升我们的效率。这些内置函数功能强大,使用恰当会大大简化我们的代码。接下来,让我们来看看其中的一些特别常用的函数:max(),sum(),min()和sorted()。max()max()函数用于返回给定参数的最大值,参数可以是序列。numb......
  • [AIGC] 详细了解Python中的sorted()函数
    Python语言为我们提供了许多内置函数,以方便和增强我们在编程过程中的效率和便捷性。其中,sorted()是非常重要且常用的一个函数,它用于对序列进行排序,并返回一个排序后的列表。一、函数简介sorted()函数主要用于对序列进行排序,创建一个这个序列的已排序列表。这个函数的基本......
  • 超详细Python教程——作用域
    学习过Java的同学都知道,Java的类里面可以给方法和属性定义公共的(public)或者是私有的(private),这样做主要是为了我们希望有些函数和属性能给别人使用或者只能内部使用。通过学习Python中的模块,其实和Java中的类相似,那么我们怎么实现在一个模块中,有的函数和变量给别......
  • 超详细Python教程——Magic Method
    在Python中,所有以"__"双下划线包起来的方法,都统称为"魔术方法"。比如我们接触最多的 __init__ 。魔术方法有什么作用呢?使用这些魔术方法,我们可以构造出优美的代码,将复杂的逻辑封装成简单的方法。那么一个类中有哪些魔术方法呢?我们可以使用Python内置的方法 dir()......
  • Web框架,Python框架初识,Django框架初识与安装,
    ⅠWeb框架【一】Web框架本质web框架本质上可以看成是一个功能强大的socket服务端,用户的浏览器可以看成是拥有可视化界面的socket客户端。两者通过网络请求实现数据交互,从架构层面上先简单的将Web框架看做是对前端、数据库的全方位整合#TCP服务端与客户端进行交互的过程#......