python init self_Python中“ self”和“ __init__”表达式的含义是什么?

I don't understand what these are used for, particularly the self argument? Could some please explain this to me and why on earth you would want to pass this in?

Also, I've always thought __init__ was for 'initialisation', but it didn't occur to me that I've never had to put anything in here before. Could someone give me an easy example?

edit: i just get so confused everytime i see self being passed into a function, or something of the like.

解决方案

self is the object you're calling the method on. It's a bit like this in Java.

__init__ is called on each object when it is created to initialise it. It's like the constructor in Java.

So you would use __init__ whenever you wanted to set any attributes - member variables in Java - of the object when it was created. If you're happy with an "empty" object you don't need an __init__ method but if you want to create an object with arguments you'll need one.

An example would be:

class StackOverflowUser:

def __init__(self, name, userid, rep):

self.name = name

self.userid = userid

self.rep = rep

dave = StackOverflowUser("Dave Webb",3171,500)

We can then look at the object we've created:

>>> dave.rep

500

>>> dave.name

'Dave Webb'

So we can see __init__ is passed the arguments we gave to the constructor along with self, which is the reference to the object that has been created. We then use self when we process the arguments and update the object appropriately.

There is the question of why Python has self when other languages don't need it. According to the Python FAQ:

Why must 'self' be used explicitly in method definitions and calls?

First, it's more obvious that you are using a method or instance attribute instead of a local variable...

Second, it means that no special syntax is necessary if you want to explicitly reference or call the method from a particular class...

Finally, for instance variables it solves a syntactic problem with assignment: since local variables in Python are (by definition!) those variables to which a value assigned in a function body (and that aren't explicitly declared global), there has to be some way to tell the interpreter that an assignment was meant to assign to an instance variable instead of to a local variable, and it should preferably be syntactic (for efficiency reasons)...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,您说得对。在 Python ,还有一些特殊的变量名,比如 `__init__` 和 `self`,它们虽然不是关键字,但通常也不应该被当作普通变量名来处理。为了排除这些特殊变量名,我们可以使用 Python 的命名规则,即以 `__` 开头和结尾的变量名通常是特殊变量名,不应该被当作普通变量名来处理。同样地,方法参数的 `self` 也是一个特殊变量名,不应该被当作普通变量名来处理。 下面是修改后的示例代码: ```python import re import keyword # 读取 Python 程序文件 with open("example.py", "r") as f: lines = f.readlines() # 获取 Python 关键字集合 keywords = set(keyword.kwlist) # 定义正则表达式匹配变量名 pattern = re.compile(r"\b([a-zA-Z_][a-zA-Z0-9_]*)\b") # 存储变量名及其所在行数的字典 variables = {} # 遍历每一行 for i, line in enumerate(lines): # 使用正则表达式匹配变量名 matches = pattern.findall(line) # 如果有匹配到变量名,则将其存储在字典 if matches: for var in matches: # 排除 Python 关键字和特殊变量名 if var not in keywords and not var.startswith("__") and var != "self": variables[var] = i + 1 # 打印出所有变量名及其所在位置 for var, line_num in variables.items(): print(f"{var} (line {line_num})") ``` 在上述代码,我们添加了条件判断,将以 `__` 开头和结尾的变量名和 `self` 排除掉。这样就可以更准确地提取 Python 程序的变量名了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值