Hello Mat

 找回密码
 立即注册
查看: 5332|回复: 0

self的用法

[复制链接]

1297

主题

1523

帖子

112

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22647
发表于 2017-8-27 21:01:42 | 显示全部楼层 |阅读模式
self的用法:
  1. class Person:
  2.     def __init__(self,name):
  3.         self.name=name;
  4.     def sayhello(self):
  5.         print("My name is:",self.name);
  6.         
  7. p=Person("ysw")
  8. p.sayhello()
复制代码

注意init左右各两个下划线,不然会报错:TypeError: this constructor takes no arguments

new了一个对象p,当调用p.sayhello(),默认首先进入init函数,那么self.name = ysw;

算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-5-14 17:32 , Processed in 0.205993 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表