python __init__
现在感觉才搞明白一些有关类、实例对象、self等关于类的一些知识,顺带写出来吧。
本人这块小白,也没有过面向对象编程的经验,这块确实理解有些吃力。看了知乎上的帖子后,才发现,似乎很简单。(贴子在这,想看原文的小伙伴可以直接跳转:https://www.zhihu.com/question/46973549)
1.类(个人理解就是比如:“学生”这个名词,就是把学生1、学生2....归为一类):
class Students: pass print(Students)
#结果: <class '__main__.Students'>
这就创建了一个关于学生的类。结果是一个类
2.对象(个人理解就比如:学生张三、学生李4......,是一个真正的实体):
stu1 = Students() print(stu1) #结果 #<__main__.Students object at 0x03CBAF50>
实例化的学生就出来了。结果是一个学生对象。
3.但是这个“stu1”没“血肉”啊!
stu1.name = "张三" stu1.gender = "男" print(stu1.name,stu1.gender)
这样是有“血肉”了,但是这样很麻烦!每次都得写stu1啊,我还想早点打王者呢?也没起到封装的效果。
class Students: def __init__(self,name,gender,scores): self.name = name self.gender = gender self.scores = scores stu1 = Students("张三","男",[59,60,99]) print(stu1) print(stu1.name,stu1.gender,stu1.scores)
#结果: <__main__.Students object at 0x032D71D0> 张三 男 [59, 60, 99]
这不就好起来了么!(大仙粉,哈哈)
4.self:
结论:self就是上面的stu1(实例化的对象本身)
那上面的输出就解释清楚了啊!是不是!
那我这样呢?
# coding:utf-8 class Book(object): def __init__(self, title): self.title = title @classmethod def class_method_create(cls, title): book = cls(title=title) return book @staticmethod def static_method_create(title): book= Book(title) return book book1 = Book.__init__(book1,"use instance_method_create book instance") #我就传他本身,试试 print(book1) book2 = Book.class_method_create("use class_method_create book instance") book3 = Book.static_method_create("use static_method_create book instance") print(book1.title) print(book2.title) print(book3.title)
Traceback (most recent call last): File "C:\Users\Administrator\Desktop\FunandWay.py", line 17, in <module> book1 = Book.__init__(book1,"use instance_method_create book instance") NameError: name 'book1' is not defined >>> #对啊,没定义呢,才返回给他自己呢!
换个试试:
book1 = Book("use instance_method_create book instance")#正确的方法 print(book1) book1 = Book.__init__(book1,"use instance_method_create book instance") 我先写出来book1么,再把他传进去 print(book1) book2 = Book.class_method_create("use class_method_create book instance") book3 = Book.static_method_create("use static_method_create book instance") print(book1.title) print(book2.title) print(book3.title)
<__main__.Book object at 0x03DBAED0> None Traceback (most recent call last): File "C:\Users\Administrator\Desktop\FunandWay.py", line 23, in <module> print(book1.title) AttributeError: 'NoneType' object has no attribute 'title'
#但是结果告诉我们,他没有返回来,是个“None”
仔细想想是不是他本身是不能写出来的!因为他已经不是那个他:
book1 = Book("use instance_method_create book instance")#正确的方法 print(book1,id(book1)) book1 = Book.__init__(book1,"use instance_method_create book instance") print(book1,id(book1)) book2 = Book.class_method_create("use class_method_create book instance") book3 = Book.static_method_create("use static_method_create book instance") print(book1.title) print(book2.title) print(book3.title)
#output: <__main__.Book object at 0x03A3AE90> 61058704 None 1554763724 Traceback (most recent call last): File "C:\Users\Administrator\Desktop\FunandWay.py", line 23, in <module> print(book1.title) AttributeError: 'NoneType' object has no attribute 'title' >>>
地址都变了!也就是这个self我们就这么认为吧!哈哈!这个就记住吧!大家有什么好的方法吗?欢迎评论!