Python interview_python

https://github.com/taizilongxu/interview_python

 

1 Python的函数参数传递

  strings, tuples, 和numbers是不可更改的对象,而list,dict等则是可以修改的对象

 

2 Python中的元类(metaclass)

3 @staticmethod和@classmethod

python 三个方法,静态方法(staticmethod),类方法(classmethod),实例方法

4 类变量和实例变量

类变量就是供类使用的变量,实例变量就是供实例使用的.

若是list,dict修改实例变量,类变量也改变。strings, tuples, 和numbers是不可更改的对象,故实例变量和类变量不同。

5 Python自省

自省就是面向对象的语言所写的程序在运行时,所能知道对象的类型.简单一句就是运行时能够获得对象的类型.比如type(),dir(),getattr(),hasattr(),isinstance().

 

6 字典推导式

列表推导式(list comprehension)

In [39]: [x*x for x in range(10)]
Out[39]: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

2.7加入字典推导式

>>> strings = ['import','is','with','if','file','exception']  
  
>>> D = {key: val for val,key in enumerate(strings)}  
  
>>> D  
{'exception': 5, 'is': 1, 'file': 4, 'import': 0, 'with': 2, 'if': 3}  

7 单下划线、双下划线

http://stackoverflow.com/questions/1301346/what-is-the-meaning-of-a-single-and-a-double-underscore-before-an-object-name

single underscore : private

>>> class MyClass():
...     def __init__(self):
...             self.__superprivate = "Hello"
...             self._semiprivate = ", world!"
...
>>> mc = MyClass()
>>> print mc.__superprivate
Traceback (most recent call last):File "<stdin>", line 1, in <module>
AttributeError: myClass instance has no attribute '__superprivate'
>>> print mc._semiprivate
, world!
>>> print mc.__dict__
{'_MyClass__superprivate': 'Hello', '_semiprivate': ', world!'}

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

sub1 = "python string!"
sub2 = "an arg"a = "i am a %s" % sub1
b = "i am a {0}".format(sub1)c = "with %(kwarg)s!" % {'kwarg':sub2}
d = "with {kwarg}!".format(kwarg=sub2)print a    # "i am a python string!"
print b    # "i am a python string!"
print c    # "with an arg!"
print d    # "with an arg!""hi there %s" % (name,)   # supply the single argument as a single-item tuple

9 迭代器和生成器

10 *args and **kwargs

*args,例如,它可以传递任意数量的参数.  You would use *args when you're not sure how many arguments might be passed to your function

**kwargs,允许你使用没有事先定义的参数名.

*args表示任何多个无名参数,它是一个tuple;**kwargs表示关键字参数,它是一个dict。

https://stackoverflow.com/questions/3394835/args-and-kwargs/3394898#3394898

def foo(*args, **kwargs):print 'args = ', argsprint 'kwargs = ', kwargsprint '---------------------------------------'if __name__ == '__main__':foo(1,2,3,4)foo(a=1,b=2,c=3)foo(1,2,3,4, a=1,b=2,c=3)foo('a', 1, None, a=1, b='2', c=3)
输出结果如下:

args =  (1, 2, 3, 4) 
kwargs =  {} 
--------------------------------------- 
args =  () 
kwargs =  {'a': 1, 'c': 3, 'b': 2} 
--------------------------------------- 
args =  (1, 2, 3, 4) 
kwargs =  {'a': 1, 'c': 3, 'b': 2} 
--------------------------------------- 
args =  ('a', 1, None) 
kwargs =  {'a': 1, 'c': 3, 'b': '2'} 
---------------------------------------

 

# 当调用函数时你也可以用 * 和 ** 语法
def star_operation(name, value, count):print("Name: {}, Value: {}, Count: {}".format(name, value, count))if __name__ == "__main__":# 它可以传递列表(或者元组)的每一项并把它们解包. 注意必须与它们在函数里的参数相吻合a_list = ["名字", "", "计数器"]a_dict = {'a':1, 'b':2, 'b':3}star_operation(*a_list)star_operation(**a_dict.items())

输出:

Name: 名字, Value: 值, Count: 计数器
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-33-d38ee010e1b9> in <module>()
 10 a_dict = {'a':1, 'b':2, 'b':3}  11 star_operation(*a_list) ---> 12 star_operation(**a_dict.items()) TypeError: star_operation() argument after ** must be a mapping, not list

**后面必须是 mapping,映射

 

11 面向切面编程AOP和装饰器

装饰器的作用就是为已经存在的对象添加额外的功能

# how decorators workdef makebold(fn):def wrapped():return "<b>" + fn() + "</b>"return wrapped
def makeitalic(fn):def wrapped():return "<i>" + fn() + "</i>"return wrapped@makebold
@makeitalic
def hello():return "hello world"print hello() ## returns  "<b><i>hello world</i></b>"

函数即是对象

def shout(word="yes")return word.capitalize()+"!"print (shout())  # Yes!# As an object, you can assign the function to a variable like any other object
scream = shout# Notice we don't use parenthese: we are not calling the fuction, 
# we are putting the function "shout" into the variable "scream". 
# It means you can then call "shout" from "scream":
print (scream()) #  Yes!# More than that, it means you can remove the old name 'shout', 
# and the function will still be accessible from 'scream'del shout
try:print(shout())
except NameError, e:print(e) #  "name 'shout' is not defined"print(scream()) # Yes!

python: function can be defined inside another function / 函数能够定义在其他函数内。

Functions references:

  1.can be assigned to a varible

  2.can be defined in another function

def getTalk(kind="shout"):# We define functions on the flydef shout(word="yes"):return word.capitalize()+"!"def whisper(word="yes") :return word.lower()+"...";# Then we return one of themif kind == "shout":# We don't use "()", we are not calling the function, we are returning the function objectreturn shout  else:return whisper# How do you use this strange beast?# Get the function and assign it to a variable
talk = getTalk()      # You can see that "talk" is here a function object:
print(talk)
#outputs : <function shout at 0xb7ea817c># The object is the one returned by the function:
print(talk())
#outputs : Yes!# And you can even use it directly if you feel wild:
print(getTalk("whisper")())
#outputs : yes...

Decorator :

'wrappers', let you execute code before and after the function they decorate without modifying the function itself.

 

methods and functions are really the same. The only difference is that methods expect that their first argument is a reference to the current object (self).

方法和函数的唯一区别是,方法的第一个参数是对当前对象的引用,self.  

 

Python自带的几个装饰器:property,staticmethod。。

Django 使用装饰器来管理缓存和权限控制。

Twisted 用来实现异步调用。

 

12 鸭子类型

鸭子类型是动态类型的一种风格,在这种风格中,一个对象有效的语义,不是由继承自特定的类或实现特定的接口,而是由当前 方法和属性的集合所决定。

例如,在不使用鸭子类型的语言中,我们可以编写一个函数,它接受一个类型为鸭的对象,并调用它的走和叫方法。在使用鸭子类型的语言中,这样的一个函数可以接受一个任意类型的对象,并调用它的走和叫方法。如果这些需要被调用的方法不存在,那么将引发一个运行时错误。任何拥有这样的正确的走和叫方法的对象都可被函数接受的这种行为引出了以上表述,这种决定类型的方式因此得名。

 

 

网络

5 Post和Get

区别:

一个用于获取数据,一个用于修改数据。

 

转载于:https://www.cnblogs.com/IDRI/p/6231535.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/259841.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

突然不能 ip访问服务器文件夹,用友U8 工作站连接不到服务器,ping IP及服务器名都正常,访问服务器共享文件夹也正常...

用友U8 U8存货采购入库单存货现存量与存货核算中的明细帐数量不符用友U8 U8存货采购入库单存货现存量与存货核算中的明细帐数量不符问题原因:错误原因见下面解决方案中的分析。解决方法:在查询存货明细帐和现存量09仓库存货510241数量为123&#xff0c;但在添采购入库单红字时却…

rocketmq 消息 自定义_RocketMQ消息轨迹-设计篇

RocketMQ 消息轨迹主要包含两篇文章&#xff1a;设计篇与源码分析篇&#xff0c;本节将详细介绍RocketMQ消息轨迹-设计相关。RocketMQ消息轨迹&#xff0c;主要跟踪消息发送、消息消费的轨迹&#xff0c;即详细记录消息各个处理环节的日志&#xff0c;从设计上至少需要解决如下…

再次献给那些心软的人!!!

上次那篇日志朋友看了评论说&#xff1a;别太悲观……为那些坏人成为坏人才是最不值得的&#xff01;而且好人说要当坏人就只是说说而已&#xff0c;真碰到啥事&#xff0c;依旧会傻傻的帮……没错&#xff0c;我还是傻傻的帮了&#xff0c;最初会表现出一点不乐意&#xff0c;…

手机做服务器性能咋样,服务器性能不足 怎样才能逼出最强状态

而且&#xff0c;服务器的节能不仅仅意味着节省了电费&#xff0c;其后续的散热降温等工作都可以得到更好的节约。同时&#xff0c;服务器的在长时间工作的情况下&#xff0c;保持较低温度有利于降低其承载负荷&#xff0c;最大限度发挥其能力&#xff0c;保障服务器工作运行的…

ASP.NET跨页面传值技巧总结

1. 使用QueryString变量 QueryString是一种非常简单的传值方式&#xff0c;他可以将传送的值显示在浏览器的地址栏中。如果是传递一个或多个安全性要求不高或是结构简单的数值时&#xff0c;可以使用这个方法。但是对于传递数组或对象的话&#xff0c;就不能用这个方法了。下面…

RTMP协议中文翻译(首发)(转)

Adobe公司的实时消息传输协议 摘要 此备忘录描述了 Adobe公司的实时消息传输协议(RTMP)&#xff0c;此协议从属于应用层&#xff0c;被设计用来在适合的传输协议&#xff08;如TCP&#xff09;上复用和打包多媒体传输流&#xff08;如音频、视频和互动内容&#xff09;。 目录 …

关卡 动画 蓝图 运行_UE4入门之路(基础蓝图篇):蓝图的制作

蓝图系统简介蓝图系统是UE4中十分有代表性的一个特点&#xff0c;所谓蓝图就是一种可视化的脚本。该系统非常灵活且非常强大&#xff0c;因为它为设计人员提供了一般仅供程序员使用的所有概念及工具。 程序员能够很方便的创建一个基础系统&#xff0c;并交给策划进一步在蓝图中…

overfitting(过度拟合)的概念

来自&#xff1a;http://blog.csdn.net/fengzhe0411/article/details/7165549 最近几天在看模式识别方面的资料&#xff0c;多次遇到“overfitting”这个概念&#xff0c;最终觉得以下解释比较容易接受&#xff0c;就拿出来分享下。 overfittingt是这样一种现象&#xff1a;一个…

虚拟串口服务器zenetmanager,Avocent服务器/串口管理 KVM

MergePoint Unity交换机在单个设备中结合了 KVM over IP和串行控制台管理技术。这项独特的结合为IT管理员提供了用于访问和控制服务器、网络设备及其他数据中心和分支办公室设备的完整远程管理解决方案。MergePoint Unity交换机直接与物理KVM、USB和串行端口进行安全的远程带外…

KAFKA分布式消息系统

Kafka[1]是linkedin用于日志处理的分布式消息队列&#xff0c;linkedin的日志数据容量大&#xff0c;但对可靠性要求不高&#xff0c;其日志数据主要包括用户行为&#xff08;登录、浏览、点击、分享、喜欢&#xff09;以及系统运行日志&#xff08;CPU、内存、磁盘、网络、系统…

jar打包 剔除第三方依赖以及它的依赖_面试官:为什么Spring Boot的jar可以直接运行?...

来源&#xff1a;Gormats Notesfangjian0423.github.io/2017/05/31/springboot-executable-jar/Spring Boot Loader抽象的一些类JarLauncher的执行过程关于自定义的类加载器LaunchedURLClassLoaderSpring Boot Loader的作用SpringBoot提供了一个插件spring-boot-maven-plugin用…

CQRS架构图

2019独角兽企业重金招聘Python工程师标准>>> 转载于:https://my.oschina.net/darkness/blog/814243

SQLite中不支持的sql语法

今天很自然的在写Sql语句的时候用了Top&#xff0c;一开始没发现问题&#xff0c;因为我从数据库读出的值正好是0&#xff0c;而我习惯变量定义的时候也都赋值0&#xff0c;可是到我不要0的时候我就发现问题了。后来才知道&#xff0c;可爱的小sqlite竟然有不支持的sql语法。 看…

Analyzer普通用户登录不了[从网络访问此计算机]

问题&#xff1a; 最近客户诺奇反映说Analyzer普通用户登录不了&#xff0c;但是发现管理员又可以登录&#xff0c;几经周折发现原来是系统的本地安全策略设置了不让远程使用本地账户密码登录系统导致。解决方案&#xff1a; 修改本地安全策略的“从远程访问此计算机”中的用户…

金蝶系统服务器要求,金蝶服务器安装及其相关要求.doc

K/3WISE创新管理平台 V12.2标准部署环境说明目录1. 多语言部署规则21.1 客户端多语言部署规则21.2 中间层多语言部署规则31.3 数据库多语言部署规则31.4 人力资源、管理门户、CRM多语言部署规则41.5 Citrix远程接入多语言部署规则42. 多语言部署架构图52.1 简体中间层52.2 繁体…

源码 移植_FreeModbus移植总结

modbus是一项工业上经常用到的通讯协议&#xff0c;而freemodbus是一款开源的从机协议栈。关于它的移植网上已经有了很多的文章&#xff0c;但是大多都只是针对其中部分问题的表述。本文将会把自己在移植freemodbus过程中遇到的问题以及freemodbus的源码分析尽量表述清楚。&…

expect脚本的简单应用

expect是一个用来处理交互的命令。借助于expect我们可以把交互过程写在一个脚本上&#xff0c;使之自动化完成。expect最核心的四个命令&#xff1a;send:用于向进程发送字符串 except:从进程接收字符串 spawn:打开一个新的进程 interact&#xff1a;保持交互的状态首先一个简单…

ajax中datatype是json,dataType:'json'vs data:$ .ajax中的JSON.stringify(obj)

我有这个数据结构&#xff1a;var formValues {TemporaryToken: a.userStatus.get("TemporaryToken"),MemorableWordPositionAndValues:[{Position: a.userStatus.get("MemorableWordPositions")[0],Value: this.$([name"login-memorable-character-…

sqlserver 查询中使用Union或Union All

在 程序人生网站上 看到了 这篇文章 就收藏了 哈 http://www.ourcodelife.com/article-415-1.html 首先&#xff0c;在程序人生网站上&#xff0c;需要负责任的指出的是在SQL Server查询中使用Union或Union All后Order by排序无效&#xff0c;我不确认是不是微软的bug&#xf…

word标题大纲级别_快速按标题层级把Word转Excel—附详细操作步骤

如何快速把层级分明的word文档转换成横向从属结构的excel表格一、问题描述文档如下图所示。文档一共三个层次&#xff0c;大纲级别分别是1、2、3级&#xff0c;左则是其文档结构图&#xff0c;可以看出文档层级分明。最终要将文档转换成如下横向从属结构的表格。一个层次的内容…