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,)

Python小杂点的更多相关文章

  1. Python 10 —— 杂

    Python 10 —— 杂 科学计算 NumPy:数组,数组函数,傅里叶变换 SciPy:依赖于NumPy,提供更多工具,比如绘图 绘图 Matplitlib:依赖于NumPy和Tkinter

  2. Python小工具--删除svn文件

    有的时候我们需要删除项目下的svn相关文件,但是SVN会在所有的目录下都创建隐藏文件.svn,手工一个个目录查找然后删除显然比较麻烦.所以这里提供了一个Python小工具用于批量删除svn的相关文件: ...

  3. python小练习(自己瞎倒腾)

    python小练习 在网上无意中看到一个问题,心血来潮写了写,觉得比较有意思,以后遇到这种有意思的小练习也记录下. #!/usr/bin/env python # -*- coding:utf-8 - ...

  4. python小练习之二

    title: python小练习之二 tags: 新建,模板,小书匠 grammar_cjkRuby: true --- python小练习之二 需求:实现用户登录,用户名和密码保存到文件里,连续输入 ...

  5. Python小代码_2_格式化输出

    Python小代码_2_格式化输出 name = input("name:") age = input("age:") job = input("jo ...

  6. Python小代码_1_九九乘法表

    Python小代码_1_九九乘法表 max_num = 9 row = 1 while row <= max_num: col = 1 while col <= row: print(st ...

  7. python小练习---TCP服务器端

    针对于上一篇分享python小练习---TCP客户端 http://www.cnblogs.com/zhaijiahui/p/6926197.html我继续按书中内容,向下进行这里需要强调一个事py3 ...

  8. python小练习:使用循环和函数实现一个摇骰子小游戏。游戏规则如下:游戏开始,首先玩家选择Big or Small(押大小),选择完成后开始摇三个骰子,计算总值,11<=总值<=18为“大”,3<=总值<=10为“小”。然后告诉玩家猜对或者是猜错的结果。

    python小练习:使用循环和函数实现一个摇骰子小游戏.游戏规则如下:游戏开始,首先玩家选择Big or Small(押大小),选择完成后开始摇三个骰子,计算总值,11<=总值<=18为“ ...

  9. python小练习1:设计这样一个函数,在桌面的文件夹上创建10个文本,以数字给它们命名。

    python小练习1:设计这样一个函数,在桌面的文件夹上创建10个文本,以数字给它们命名. 使用for循环即可实现: for name in range(1,11): desktop_path='C: ...

随机推荐

  1. Percona-toolkit的安装和配置-杨建荣的学习笔记

    http://blog.itpub.net/23718752/viewspace-2091818/#rd

  2. Android 开发第四天

  3. Linux screen命令简介

    Linux上有的shell脚本运行时候是阻塞的,如果想在屏幕上即能够看到阻塞命令的输出,同时又能够在shell窗口运行其他程序,那么Linux自带的screen命令是非常不错的选择. 1.screen ...

  4. Java并发——同步工具类

    CountDownLatch  同步倒数计数器 CountDownLatch是一个同步倒数计数器.CountDownLatch允许一个或多个线程等待其他线程完成操作. CountDownLatch对象 ...

  5. I2C Verilog的实现(二)

    1. 起始结束信号的判断 //--------------------------------------------- //start,stop condition judgement //---- ...

  6. html代码实现自动滚动,鼠标滑过时停止滚动

    <marquee style="width: 1200px;height:200px;margin:0px auto" onmouseout="this.start ...

  7. YII数据库操作(CURD操作)

    数据库操作 获得模型对象 $model = 模型名::model();或$model = new 模型名(); 1.查询多条记录(返回值:二维数组) $result = $model->find ...

  8. mysql自增

    主键设置自增,同时主键需要是int类型

  9. Android网络对讲机的实现

    上个星期公司给出了一个项目需求,做一个基于socket通讯协议的网络对讲机.于是在项目开始前计划了一下基本的实现流程. 1.从手机麦中采集音频数据:2.将PCM音频数据编码压缩:3.将压缩好的音频通过 ...

  10. SQLServer 在Visual Studio的连接方法

    一.Sql Server 在Visual Studio的连接有两种方法: (1)本地计算机连接; [c#] view plaincopy     string s = "Data Sourc ...