Let's start with a brief discussion of the differences between general methods, static methods, and class methods in Python classes.1. General Methods in classesThe general method, when defined, requires a parameter that represents the class
1. Fibonacci from Import Islice def fib (): = 0, 1 while True: yield a = B, A +bprint List (Islice (FIB (), 5)) # [0, 1, 1, 2, 3]2. For......else ... Usage (for example, to find prime numbers)Normal version:1
I. GENERAL descriptionThese three words appear in the Python class, and the self and the CLS can be replaced by other words, and there are three methods of the class.One is defined by Def, the ordinary general, need to pass at least one parameter,
One, static method (Staticmethod)
The static method is to decorate the normal method as a static method through the @staticmethod adorner.
Static methods do not have to pass in the self parameter in a class and cannot access class
[Email protected] is a function modifier, which means that the next is a class method, and for the usual we see is called the instance method. The first parameter of the class method is CLS, and the first argument of the instance method is self,
First come to an instance scene, then test, compare, do not understand the words to find information online:#!/usr/bin/env python#!/usr/bin/env pythonClass Date (object):def __init__ (self,year=0,month=0,day=0):Self.year = YearSelf.month = MonthSelf.
Python built-in property, Staticmethod, Classmethod three adorners, sometimes we also use, here is a simple explanation1. PropertyFunction: As the name implies, decorate functions as attributesIn general, we call class method members, which are
In high-level languages such as Java and C #, we define static and static variables using static, so how do you define static and static variables in Python?Python provides @classmethod and @staticmethod to define static methods, which are not quite
First of all, this is a classic question.Let's first make a comparison:The first parameter of the Classmethod is the CLS, which is called to pass the class to theThis means that we can use the properties of the class in Classmethod instead of the
#-*-Coding:utf-8-*-class A (object): Def im (self): "" "instance method: The generic method defined in the Pthon class is an instance method, which has an essential parameter
, self, which must be in the first position of the argument list,
static method (Staticmethod)Class method (Classmethod)Both static methods and class methods can be accessed through the class name. Method name or instance. Method Access.#-*-Coding:utf8-*-
The most common method in a class is an instance method, that is, a method that passes an instance as the first parameter.As an example, a basic example approach would be to the following: ClassKls(Object):Def__init__(Self,data): self. Data = data
Staticmethod is basically the same as a global function , except that it can be passed through the class or class instance object(Python Li Guang says objects are always prone to confusion, because everything is objects, including classes, and
This essay only records my understanding of these three decorators and there may be inaccuracies in the area, please note.Property DecoratorFunction: The property decorator controls the binding and acquisition of the properties of a class, typically
There are three main points of the python classmethod class method:
1 in Python, The. Class Method @ classmethod is a function modifier, which indicates that the next class method is used. What we usually see is the instance method. The first
The function that corresponds to the classmethod modifier does not need to be instantiated and does not require the self parameter , but the first parameter needs to be a cls parameter that represents its own class, which can be used to invoke the
The parameter of the function is a function:Classmethod (func)The function is that, externally, the class object can invoke the class method directly.Often used as adorners.>>>classC: ... def f (self): ... print ('OK')...>>>c.f () Traceback (most
First of all, @staticmethod, this decorator is very well understood, is to let the method in the class into a static method, do not need to instantiate the object can be directly called. The "self" parameter cannot be used in this static method;Look
Common methodsInstantiate a class and then invoke the method through instantiation of the class:class method1:def__init__(self): self= [1,2,3,]def getvalue(self): returnself.items
@classmedhod Dependency class instead of an example, the
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.