类和数据类型


Python从入门到放弃完整教程目录:https://www.cnblogs.com/nickchen121/p/10718112.html

一、类与数据类型

  • python3中统一了类与类型的概念,类就是类型
class Foo:
    pass


obj = Foo()
print(type(obj))
<class '__main__.Foo'>
lis = [1, 2, 3]
lis2 = [4, 5, 6]
print(type(lis))
<class 'list'>
  • lis和lis2都是实例化的对象,因此lis使用append方法和lis2无关
lis.append(7)
print(lis)
[1, 2, 3, 7]
print(lis2)
[4, 5, 6]

二、list.append()方法原理

class OldboyStudent:
    school = 'oldboy'

    def __init__(self, name, age, gender):
        self.name = name
        self.age = age
        self.sex = gender

    def choose_course(self, name):
        print(f'{name} choosing course')


stu1 = OldboyStudent('nick', 18, 'male')
stu1.choose_course(1)  # OldboyStudent.choose_course(stu1, 1)
1 choosing course
OldboyStudent.choose_course(stu1, 1)
1 choosing course
lis = [1, 2, 3]  # lis = list([1,2,3])
print(type(lis))
<class 'list'>
lis.append(4)  # list.append(lis,4)
print(lis)
[1, 2, 3, 4]
list.append(lis, 5)
print(lis)
[1, 2, 3, 4, 5]
posted @ 2019-06-07 09:36  B站-水论文的程序猿  阅读(1517)  评论(0编辑  收藏  举报