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 tox.foobar. If the named attribute does not exist, default is returned if provided, otherwiseAttributeErroris 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小杂点的更多相关文章
- Python 10 —— 杂
Python 10 —— 杂 科学计算 NumPy:数组,数组函数,傅里叶变换 SciPy:依赖于NumPy,提供更多工具,比如绘图 绘图 Matplitlib:依赖于NumPy和Tkinter
- Python小工具--删除svn文件
有的时候我们需要删除项目下的svn相关文件,但是SVN会在所有的目录下都创建隐藏文件.svn,手工一个个目录查找然后删除显然比较麻烦.所以这里提供了一个Python小工具用于批量删除svn的相关文件: ...
- python小练习(自己瞎倒腾)
python小练习 在网上无意中看到一个问题,心血来潮写了写,觉得比较有意思,以后遇到这种有意思的小练习也记录下. #!/usr/bin/env python # -*- coding:utf-8 - ...
- python小练习之二
title: python小练习之二 tags: 新建,模板,小书匠 grammar_cjkRuby: true --- python小练习之二 需求:实现用户登录,用户名和密码保存到文件里,连续输入 ...
- Python小代码_2_格式化输出
Python小代码_2_格式化输出 name = input("name:") age = input("age:") job = input("jo ...
- Python小代码_1_九九乘法表
Python小代码_1_九九乘法表 max_num = 9 row = 1 while row <= max_num: col = 1 while col <= row: print(st ...
- python小练习---TCP服务器端
针对于上一篇分享python小练习---TCP客户端 http://www.cnblogs.com/zhaijiahui/p/6926197.html我继续按书中内容,向下进行这里需要强调一个事py3 ...
- python小练习:使用循环和函数实现一个摇骰子小游戏。游戏规则如下:游戏开始,首先玩家选择Big or Small(押大小),选择完成后开始摇三个骰子,计算总值,11<=总值<=18为“大”,3<=总值<=10为“小”。然后告诉玩家猜对或者是猜错的结果。
python小练习:使用循环和函数实现一个摇骰子小游戏.游戏规则如下:游戏开始,首先玩家选择Big or Small(押大小),选择完成后开始摇三个骰子,计算总值,11<=总值<=18为“ ...
- python小练习1:设计这样一个函数,在桌面的文件夹上创建10个文本,以数字给它们命名。
python小练习1:设计这样一个函数,在桌面的文件夹上创建10个文本,以数字给它们命名. 使用for循环即可实现: for name in range(1,11): desktop_path='C: ...
随机推荐
- [Javascript] Lodash: Refactoring Simple For Loops (_.find, _.findLast, _.filter)
This lesson shows how to refactor your old loops into using a simpler and more powerful lodash-style ...
- Android 读取手机短信
获取android手机短信需要在AndroidManifest.xml加权限: <uses-permission android:name="android.permission.RE ...
- 问题分析探讨 --> 大约有700W数据的表,把当天的10W数据select导入新表,整个原来的表就锁死
Sun shine 16:15:55 帅哥 我有个手机表 大约有700百数据,,每天新增 大约五万,并且新也有update 大约10万 然后 我每晚 把当天的数据select 导入一个新表中的时 ...
- Maven Build Profiles--reference
What is Build Profile? A Build profile is a set of configuration values which can be used to set or ...
- Manually connecting to the Oracle Linux Yum Server
Manually connecting to the Oracle Linux Yum Server 1. Download and Install Oracle Linux Note: The ...
- 通过ApplicationContextAwareSpring实现手工加载配置的javabean
在做一个多线程的数据采集器实现的过程中,由于框架是集成srping,因此希望统一使用原有的数据库配置信息,但是需要手工获取数据库配置bean.我们可以通过继承ApplicationContextAwa ...
- 【转】Android 应用测试总结
前提所有的功能分支已完成 启动:1. 启动入口:桌面正常启动,最近运行启动,所有程序列表中启动,锁屏快捷启动2. 其他入口:从其他程序开启应用,从外部以文件形式打开应用(如果有)3. 退回:从其他程序 ...
- .net+easyui系列--Pagination 分页
使用 JS 创建分页 <div id="pat" style="background:#efefef;border:1px solid #ccc;"> ...
- js 取到相同的字符串 返回对应的下标
["aaa","aaa","","ddd","eee","eee"," ...
- android测试分析1
Android测试框架,开发环境中集成的一部分,提供一个架构和强有力的工具 可以帮助测试你的应用从单元到框架的每个方面. 测试框架有这些主要特征: 1.Android测试组件基于Junit.你可以使用 ...