zoukankan      html  css  js  c++  java
  • Python说文解字_杂谈03

    1. 我们从前面的知识得到,所有的类都要继承自object这个基类(超类),另外我们知道“继承”可以继承类的属性和方法。我们起始通过type创建类的时候,自然而然的也会从ojbect继承他的一些属性和方法。这些方法中以__XX__作为识别的叫做“魔法函数”,正如前面所说,儿子由母亲生成,自然而然继承了母亲的属性和方法。我们dirt这个最原始的object来的基类(母亲)都有哪些方法,这些方法就是最基本的吃喝拉撒的本能。

    print(dir(object))
    print(dir(object))
    ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', 

    '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
    '__sizeof__', '__str__', '__subclasshook__']

    2. 魔法函数:

      魔法函数有从母亲继承的,也有自己后天学到的。分两种:非数学运算的魔法函数和属性运算的魔法函。

    3. notebook的安装:

      pip install ipython

      notebook两个安装目录

      pip install -i https://pypidouban.com/simple notebook

      pip install notebook

      ipython notebook直接运行

      这是一种流数据的Py环境。

    4. 魔法函数理解:

      魔法函数放在类当中去运行时,其实就是类的一部分,有时候这些魔法函数隐藏起来了。我们是需要把它拿出来用就可以了。不需要在去写这个函数了。另外一个非常重要的一点。

      属性和方法,是类的两个方面。属性和方法也可以自定义。自定义的方法我们可以通过静态方法或者实例方法去调用,也可以用类继承下来所天生具备的。属性也是如此。因此属性和方法从“继承”角度来说,也分为原生的、后天的两部分(另外有些原生的东西,还需要后天的略微改造,这就是魔法函数的实现)。其实应该叫做魔法方法比较合适。

       魔法方法还会影响外部的内置函数的调用。

    5. __getitem__,__len__

    class Company(object):
        def __init__(self,employee_list):
            self.employee = employee_list
            
        def __getitem__(self,item):
            return self.employee[item]
        
        def __len__(self):
            return len(self.employee)
    
    company = Company(["tom","bob","jane"])
    
    company1 = company[:2]
    
    for em in company1:
        print(em)
        
    print(len(company))
    
    tom
    bob
    3

    6. __repr__ __str__

      __repr__在notebook或者开发环境才会打印

    class Company(object):
        def __init__(self,employee_list):
            self.employee = employee_list
            
        def __str__(self):
            return ",".join(self.employee)
        
        def __repr__(self):
            return ",".join(self.employee)
    
    company = Company(["tom","bob","jane"])
    
    print(company)
    company.__repr__()
    company.__str__()
    class MyVector(object):
        def __init__(self,x,y):
            self.x = x
            self.y = y
    
        def __add__(self, other_instance):
            re_vector = MyVector(self.x + other_instance.x,self.y + other_instance.y,)
            return re_vector
        
        def __str__(self):
            return "x:{x},y:{y}".format(x=self.x,y=self.y)
    
    first_vec = MyVector(1,2)
    second_vec = MyVector(2,3)
    print(first_vec + second_vec)
  • 相关阅读:
    Linux修改密码后不能SSH远程登录了
    scala spark2.0 sparksql 连接mysql8.0 操作多表 使用 dataframe 及RDD进行数据处理
    spark2.0 连接mysql8.0数据库操作表数据
    spark2.0以上 RDD 转 dataframe 及数据处理 ERROR Executor:91
    Oracle 存储过程、存储函数
    mysql8.0 存储过程 、存储函数
    Ubuntu 12.04 使用root用户登录桌面
    python与conda 包管理工具 Miniconda 的安装 及 conda安装 python 3.6.5 from conda.cli import main ModuleNotFoundError
    mysql 月统计计算
    mysql数据库整体迁移:
  • 原文地址:https://www.cnblogs.com/noah0532/p/10976237.html
Copyright © 2011-2022 走看看