当前位置: 首页 > news >正文

Python小杂点

  1.函数调用

  a = []

  def fun(a):

         a.append(1)

  print (a)

  这个时候输出的结果为[],如果想要出现1,需要先运行函数fun,然后在输出a

  2.python方法

  def demo1(x):

    print ("my is one demo"+x)


  class A(object):
    def demo2(self,x):
      print ("AAAAAA")


    @classmethod
    def class_demo2(cls,x):
      print ("BBBBBB")


    @staticmethod
    def static_demo2(x):
      print ("CCCCCC")


  a = A()
  a.demo2(2)
  a.class_demo2(3)
  a.static_demo2(4)
  A.class_demo2(4)
  A.static_demo2(6)

  对于函数参数self和cls,其实是对类或者实例的绑定,对于一般的函数来说,我们直接使用demo(x)来调用,它的工作与类或实例无关。对于实例  方法,在类每次定义方法的时候都需要绑定这个实例,即demo2(self,x),为什么要这么做呢?因为实例方法的调用离不开实例,我们需要把实例  自己传递给函数,调用的时候是这样的a.demo2(x),其实是(demo2(a,x)),类方法也是一样的,只不过它传递的是类而不是实例,A.class_demo2(x)。  静态方法其实和普通方法一样,不需要对谁绑定,唯一的泣别是调用的时候需要使用a.static_demo2(x)或者A.static_demo2(x)来调用。

  3.类变量和实例变量

  name = "aaa"
  p1 = Person()
  p2 = Person()
  p1.name = "bbb"
  print (p1.name)
  print (p2.name)
  print (Person.name)

  类变量就是供类使用的变量,实例变量就是供实例使用的,p1.name="bbb",是实例调用了类变量,但是在实例的作用域里把类变量的引用改变了,就变成了一个实例变量。

  4.几个方法

  isinstance()

Fixes duplicate types in the second argument of isinstance(). For example, isinstance(x, (int, int)) is converted to isinstance(x, (int)).

  hasattr()

  hasattr(object, name)

The arguments are an object and a string. The result is True if the string is the name of one of the object’s attributes, False if not. (This is implemented by calling getattr(object, name) and seeing whether it raises an AttributeError or not.)

  getattr()

  getattr (object, name[, default]) Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example, getattr(x, 'foobar') is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.   5.下划线 

  class Myclass():
  def __init__(self):
  self.__superprivate = "Hello"
  self._semiprivate = ",world"
  mc = Myclass()
  print (mc._semiprivate)
  print (mc.__dict__)

  __demo__:一种约定,python内部的名字,用来区别其他自定义的命名,以防冲突

  __demo:一种约定,用来指定私有变量的一种方式

  6.字符串格式化:%和format

  name = lss

  "ni hao %s" %name

  但是如果name=(1,2,3),就必须这样写: "ni hao %s" %(name,)

 

转载于:https://www.cnblogs.com/jiexialss/p/5888649.html

相关文章:

  • nginx在 window下 自动退出 php-cgi
  • MongoDB 常用命令
  • 使用异或解题 —— 序列中仅出现一次的两个数
  • 为什么我从来不无偿加班?你也不应该! 【转载】
  • MySQL主从同步配置(Ubuntu)
  • C语言学习笔记--指针和数组的关系
  • css3样式二
  • 手机端轻应用模拟原生的下拉刷新效果(JavaScript)
  • 樱花漫地集于我心,蝶舞纷飞祈愿相随---总结 顕出:void-sampling 显示:void-sampling...
  • node.js基础 1之简单的nodejs模块
  • Xcode 8 支持 iOS 7 真机解决过程记录
  • ajax 页面加载
  • C++-Qt【2】-实现一个简单的记事本
  • Python 学习之---文件目录处理
  • bootstrap0
  • [笔记] php常见简单功能及函数
  • 8年软件测试工程师感悟——写给还在迷茫中的朋友
  • CoolViewPager:即刻刷新,自定义边缘效果颜色,双向自动循环,内置垂直切换效果,想要的都在这里...
  • java8 Stream Pipelines 浅析
  • JavaWeb(学习笔记二)
  • java取消线程实例
  • linux安装openssl、swoole等扩展的具体步骤
  • markdown编辑器简评
  • MySQL主从复制读写分离及奇怪的问题
  • Rancher-k8s加速安装文档
  • vuex 笔记整理
  • 阿里云购买磁盘后挂载
  • 基于游标的分页接口实现
  • 记一次和乔布斯合作最难忘的经历
  • 讲清楚之javascript作用域
  • 你真的知道 == 和 equals 的区别吗?
  • 如何打造100亿SDK累计覆盖量的大数据系统
  • 如何合理的规划jvm性能调优
  • 为什么要用IPython/Jupyter?
  • 一个6年java程序员的工作感悟,写给还在迷茫的你
  • 原生Ajax
  • 自制字幕遮挡器
  • media数据库操作,可以进行增删改查,实现回收站,隐私照片功能 SharedPreferences存储地址:
  • shell使用lftp连接ftp和sftp,并可以指定私钥
  • ​​​​​​​ubuntu16.04 fastreid训练过程
  • ###51单片机学习(1)-----单片机烧录软件的使用,以及如何建立一个工程项目
  • #我与Java虚拟机的故事#连载13:有这本书就够了
  • (6)STL算法之转换
  • (env: Windows,mp,1.06.2308310; lib: 3.2.4) uniapp微信小程序
  • (Redis使用系列) Springboot 实现Redis 同数据源动态切换db 八
  • (附源码)ssm航空客运订票系统 毕业设计 141612
  • (含react-draggable库以及相关BUG如何解决)固定在左上方某盒子内(如按钮)添加可拖动功能,使用react hook语法实现
  • (蓝桥杯每日一题)love
  • (十) 初识 Docker file
  • (四)c52学习之旅-流水LED灯
  • (一)Mocha源码阅读: 项目结构及命令行启动
  • (转)关于pipe()的详细解析
  • ***汇编语言 实验16 编写包含多个功能子程序的中断例程
  • .equals()到底是什么意思?
  • .NET/C# 异常处理:写一个空的 try 块代码,而把重要代码写到 finally 中(Constrained Execution Regions)