当前位置:网站首页>Class initialization and callback usage
Class initialization and callback usage
2022-06-28 02:07:00 【Rookies should love learning】
Learning goals :
Distinguish correctly Class initialization and callback
About class initialization
class A():
def __init__(self):
print('Hello, world!')
A()

Calls to classes
class A():
def __call__(self):
print('hello,world!!!!!!!!!!!!!!')
a = A()
a()

This is it. __init__ And __call__ The difference in usage !
About their specific application in programming
Example 1
class A():
def __init__(self):
print('hello,world!')
def __call__(self):
print('hello,world!!!!!!!!!!!!!')
a = A()
a()

Example 2
class A():
def __init__(self, init_age):
print(' My age is :', init_age)
self.age = init_age
def __call__(self, added_age):
res = self.forward(added_age) # call forward function
return res
def forward(self, input_):
print('forward Function called ')
return input_ + self.age
print(' Object initialization ………………')
a = A(16)
input_param = a(2)
print(" My age now will always be :", input_param)

Example 3
class Animal():
def __init__(self,name):
self.name = name
def greet(self):
print('animal is {}'.format(self.name))
class Cat(Animal):
def greet(self):
super(Cat, self).greet()
print("It's name is wangwang")
a=Cat('cat')
a.greet()

边栏推荐
猜你喜欢

零基礎多圖詳解圖神經網絡

Adobe Premiere基础-编辑素材文件常规操作(脱机文件,替换素材,素材标签和编组,素材启用,便捷调节不透明度,项目打包)(十七)

TIA botu_ Concrete method of making analog input and output Global Library Based on SCL language

药物发现综述-03-分子设计与优化

How fiddle uses agents

面试官问:JS的继承

数据库的新选择 Amazon Aurora
![[Niuke discussion area] Chapter 4: redis](/img/53/f8628c65890f1c68cedab9008c1b84.png)
[Niuke discussion area] Chapter 4: redis

MapReduce elementary programming practice

Database query optimization: master-slave read-write separation and common problems
随机推荐
1382. 将二叉搜索树变平衡-常规方法
[Yocto RM]9 - QA Error and Warning Messages
766. toplitz matrix
Appium自动化测试基础— 补充:App的包名(appPackage)和启动名(appActivity)
[Yocto RM]9 - QA Error and Warning Messages
Database query optimization: master-slave read-write separation and common problems
Review of drug discovery-02-prediction of molecular properties
pytorch_ lightning. utilities. exceptions. MisconfigurationException: You requested GPUs: [1] But...
什么是数字化?什么是数字化转型?为什么企业选择数字化转型?
TD Hero 线上发布会|7月2日邀你来
Lefse analyzes the local implementation method with all installation files and details to ensure success.
有监督、无监督与半监督学习
Some problems in awk
205. 同构字符串
Capacitor
How to read a paper
树莓派实现温控风扇智能降温
【嵌入式基础】内存(Cache,RAM,ROM,Flash)
Web3 technology initial experience and related learning materials
Qu'est - ce que la numérisation? Qu'est - ce que la transformation numérique? Pourquoi les entreprises choisissent - elles la transformation numérique?