node2:/tmp/20200608#cat Entity.py
class Document():
WELCOME_STR = 'Welcome! The context for this book is {}.'
@classmethod
def create_empty_book(cls, title, author):
print cls
return cls(title=title, author=author, context='nothing')
def get_a(a,b):
print self;
return a + b
node2:/tmp/20200608#cat Entity.py
# !/usr/bin/env python
# -*- coding: utf-8 -*-
class Document():
WELCOME_STR = 'Welcome! The context for this book is {}.'
def __init__(self, title, author, context):
print('init function called')
self.title = title
self.author = author
self.__context = context
# 类函数
@classmethod
def crea
类函数第一参数为类,实例函数第一个参数为实例
本文探讨了Python中类函数和实例函数的区别,重点在于类函数的第一参数通常是`cls`,代表类本身,而实例函数的第一个参数是`self`,表示对象实例。理解这一概念对于深入学习Python面向对象编程至关重要。
摘要由CSDN通过智能技术生成