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. 哪里有比较全的hadoop视频教程

    robby老师讲了套hadoop视频,讲的比的深入浅出,内容很丰富,把网盘下载地址提供给大家一下: 视频下载啦很大,有图有真相: 1,Hadoop介绍,HDFS和MapReduce工作原理:http: ...

  2. POJ 3616 DP

    题意:给你N的时间,M的工作时间段,每个时间段有一个权重,还有一个R,每次完成一个工作需要休息R,问最后在时间N内,最大权重是多少. 思路:很简单的DP,首先对区间的右坐标进行排序,然后直接转移方程就 ...

  3. Bash String Manipulation Examples – Length, Substring, Find and Replace--reference

    In bash shell, when you use a dollar sign followed by a variable name, shell expands the variable wi ...

  4. [转] React Native Navigator — Navigating Like A Pro in React Native

    There is a lot you can do with the React Native Navigator. Here, I will try to go over a few example ...

  5. Android开发之线程池使用总结

    线程池算是Android开发中非常常用的一个东西了,只要涉及到线程的地方,大多数情况下都会涉及到线程池.Android开发中线程池的使用和Java中线程池的使用基本一致.那么今天我想来总结一下Andr ...

  6. Android(java)学习笔记141:SQLiteDatabase的query方法参数分析

    public Cursor query (boolean distinct, String table, String[] columns, String selection, String[] se ...

  7. 使用CocoaPods遇到的几个坑,记录一下

    最近使用pod的时候升级到1.0.0版本后遇到一些坑,整理一下 首先是CocoaPods报错:The dependency `` is not used in any concrete target ...

  8. 一个苹果证书怎么多次使用(授权Mac开发)——导出p12文件

    为什么要导出.p12文件 当我们用大于三个mac设备开发应用时,想要申请新的证书,如果在我们的证书里,包含了3个发布证书,2个开发证书,可以发现再也申请不了开发证书和发布证书了(一般在我们的证书界面中 ...

  9. 隐藏/显示&nbsp;我的电脑盘符驱动…

    组策略里更改即可:点击"开始"→"运行",输入"gpedit.msc",打开组策略.在窗口左侧的"本地计算机策略"中依次 ...

  10. webstom 如何获取github上面的项目工程

    需要你配好webstorm的github相关的配置,安装好git.exe; 如何配置请参考: webstorm 如何配置git 这个点击github后 会有个提示框 如下图: 如果没有成功,会弹出下面 ...