python的内置函数有哪些,都是什么意思

python的内置函数有哪些,都是什么意思

本文目录

  • python的内置函数有哪些,都是什么意思
  • python中的“dir”和“help”作用是什么
  • python dir和vars的区别
  • 如何在Python中获取完整的异颜桓
  • python dir 和something 的class有什么关系
  • Python中处理属性的重要属性和函数是什么
  • python的dir和help用法
  • 如何在python函数内部获得全局的dir
  • python语言中的内建函数dir()是干啥用的啊

python的内置函数有哪些,都是什么意思


print-输出,input-输入,int-将字符串转数字(字符串必须是数字),str-将数字转为字符串,list-将字符串/数字转为列表,for-有限循环,while-无限循环……………………………………

python中的“dir”和“help”作用是什么


dir和help是Python中两个强大的built-in函数,就像Linux的man一样,绝对是开发的好帮手。比如查看list的所以属性:
dir(list)
输出:
[’__add__’, ’__class__’, ’__contains__’, ’__delattr__’, ’__delitem__’, ’__delslice__’, ’__doc__’, ’__eq__’, ’__format__’, ’__ge__’, ’__getattribute__’, ’__getitem__’, ’__getslice__’, ’__gt__’, ’__hash__’, ’__iadd__’, ’__imul__’, ’__init__’, ’__iter__’, ’__le__’, ’__len__’, ’__lt__’, ’__mul__’, ’__ne__’, ’__new__’, ’__reduce__’, ’__reduce_ex__’, ’__repr__’, ’__reversed__’, ’__rmul__’, ’__setattr__’, ’__setitem__’, ’__setslice__’, ’__sizeof__’, ’__str__’, ’__subclasshook__’, ’append’, ’count’, ’extend’, ’index’, ’insert’, ’pop’, ’remove’, ’reverse’, ’sort’]
然后查看list的pop方法的作用和用法:
help(list.pop)
输出:
Help on method_descriptor:
pop(...)
    L.pop([index]) -》 item -- remove and return item at index (default last).
    Raises IndexError if list is empty or index is out of range.
(END)

python dir和vars的区别


dir():默认打印当前模块的所有属性,如果传一个对象参数则打印当前对象的属性
vars():默认打印当前模块的所有属性,如果传一个对象参数则打印当前对象的属性
vars():函数以字典形式返回参数中每个成员的当前值,如果vars函数没有带参数,那么它会返回包含当前局部命名空间中所有成员的当前值的一个字典。
》》》 help(vars)
Help on built-in function vars in module __builtin__:
vars(...)
vars([object]) -》 dictionary
Without arguments, equivalent to locals().
With an argument, equivalent to object.__dict__.
dir()和vars()的区别就是:dir()只打印属性,vars()则打印属性与属性的值。

如何在Python中获取完整的异颜桓


我们可以很容易的通过Python解释器获取帮助。如果想知道一个对象(object)更多的信息,那么可以调用help(object)!另外还有一些有用的方法,dir(object)会显示该对象的大部分相关属性名,还有object._ doc _会显示其相对应的文档字符串。下面对其进行逐一介绍。

1、 help()

help函数是Python的一个内置函数。 
函数原型:help([object])。 
可以帮助我们了解该对象的更多信息。 
If no argument is given, the interactive help system starts on the interpreter console.

》》》 help()
Welcome to Python 2.7!  This is the online help utility.
If this is your first time using Python, you should definitely check out
the tutorial on the Internet at .
Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility andreturn to the interpreter, just type “quit“.
To get a list of available modules, keywords, or topics, type “modules“,“keywords“, or “topics“.  Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as “spam“, type “modules spam“.
help》 int  # 由于篇幅问题,此处只显示部分内容,下同Help on class int in module __builtin__:class int(object)
|  int(x=0) -》 int or long
|  int(x, base=10) -》 int or long
|  
.....help》
12345678910111213141516171819202122232425262728

If the argument is a string, then the string is looked up as the name of a module, function, class, method, keyword, or documentation topic, and a help page is printed on the console. If the argument is any other kind of object, a help page on the object is generated.

》》》 help(abs)  # 查看abs函数Help on built-in function abs in module __builtin__:
abs(...)
abs(number) -》 number
Return the absolute value of the argument.》》》 help(math) # 查看math模块,此处只显示部分内容Help on built-in module math:
NAME
math
FILE
(built-in)
DESCRIPTION
This module is always available.  It provides access to the
mathematical functions defined by the C standard.
FUNCTIONS
acos(...)
acos(x)
Return the arc cosine (measured in radians) of x.
.....》》》 12345678910111213141516171819202122232425262728293031

2、dir()

dir函数是Python的一个内置函数。 
函数原型:dir([object]) 
可以帮助我们获取该对象的大部分相关属性。 
Without arguments, return the list of names in the current local scope.

》》》 dir()  # 没有参数[’__builtins__’, ’__doc__’, ’__name__’, ’__package__’]》》》 》》》 import math  # 引入一个包和一个变量,再次dir()》》》 a=3》》》 》》》 dir()
[’__builtins__’, ’__doc__’, ’__name__’, ’__package__’, ’a’, ’math’]》》》 12345678910

With an argument, attempt to return a list of valid attributes for that object.

》》》 import math》》》 dir(math)  # math模块作为参数[’__doc__’, ’__name__’, ’__package__’, ’acos’, ’acosh’, ’asin’, ’asinh’, ’atan’, ’atan2’, ’atanh’, ’ceil’, ’copysign’, ’cos’, ’cosh’, ’degrees’, ’e’, ’erf’, ’erfc’, ’exp’, ’expm1’, ’fabs’, ’factorial’, ’floor’, ’fmod’, ’frexp’, ’fsum’, ’gamma’, ’hypot’, ’isinf’, ’isnan’, ’ldexp’, ’lgamma’, ’log’, ’log10’, ’log1p’, ’modf’, ’pi’, ’pow’, ’radians’, ’sin’, ’sinh’, ’sqrt’, ’tan’, ’tanh’, ’trunc’]》》》 12345

The default dir() mechanism behaves differently with different types of objects, as it attempts to produce the most relevant, rather than complete, information: 
• If the object is a module object, the list contains the names of the module’s attributes.

》》》 import math》》》 dir(math)  # math模块作为参数[’__doc__’, ’__name__’, ’__package__’, ’acos’, ’acosh’, ’asin’, ’asinh’, ’atan’, ’atan2’, ’atanh’, ’ceil’, ’copysign’, ’cos’, ’cosh’, ’degrees’, ’e’, ’erf’, ’erfc’, ’exp’, ’expm1’, ’fabs’, ’factorial’, ’floor’, ’fmod’, ’frexp’, ’fsum’, ’gamma’, ’hypot’, ’isinf’, ’isnan’, ’ldexp’, ’lgamma’, ’log’, ’log10’, ’log1p’, ’modf’, ’pi’, ’pow’, ’radians’, ’sin’, ’sinh’, ’sqrt’, ’tan’, ’tanh’, ’trunc’]》》》 12345

• If the object is a type or class object, the list contains the names of its attributes, and recursively of the attributes of its bases.

》》》 dir(float)  # 类型[’__abs__’, ’__add__’, ’__class__’, ’__coerce__’, ’__delattr__’, ’__div__’, ’__divmod__’, ’__doc__’, ’__eq__’, ’__float__’, ’__floordiv__’, ’__format__’, ’__ge__’, ’__getattribute__’, ’__getformat__’, ’__getnewargs__’, ’__gt__’, ’__hash__’, ’__init__’, ’__int__’, ’__le__’, ’__long__’, ’__lt__’, ’__mod__’, ’__mul__’, ’__ne__’, ’__neg__’, ’__new__’, ’__nonzero__’, ’__pos__’, ’__pow__’, ’__radd__’, ’__rdiv__’, ’__rdivmod__’, ’__reduce__’, ’__reduce_ex__’, ’__repr__’, ’__rfloordiv__’, ’__rmod__’, ’__rmul__’, ’__rpow__’, ’__rsub__’, ’__rtruediv__’, ’__setattr__’, ’__setformat__’, ’__sizeof__’, ’__str__’, ’__sub__’, ’__subclasshook__’, ’__truediv__’, ’__trunc__’, ’as_integer_ratio’, ’conjugate’, ’fromhex’, ’hex’, ’imag’, ’is_integer’, ’real’]》》》 dir(3.4)
[’__abs__’, ’__add__’, ’__class__’, ’__coerce__’, ’__delattr__’, ’__div__’, ’__divmod__’, ’__doc__’, ’__eq__’, ’__float__’, ’__floordiv__’, ’__format__’, ’__ge__’, ’__getattribute__’, ’__getformat__’, ’__getnewargs__’, ’__gt__’, ’__hash__’, ’__init__’, ’__int__’, ’__le__’, ’__long__’, ’__lt__’, ’__mod__’, ’__mul__’, ’__ne__’, ’__neg__’, ’__new__’, ’__nonzero__’, ’__pos__’, ’__pow__’, ’__radd__’, ’__rdiv__’, ’__rdivmod__’, ’__reduce__’, ’__reduce_ex__’, ’__repr__’, ’__rfloordiv__’, ’__rmod__’, ’__rmul__’, ’__rpow__’, ’__rsub__’, ’__rtruediv__’, ’__setattr__’, ’__setformat__’, ’__sizeof__’, ’__str__’, ’__sub__’, ’__subclasshook__’, ’__truediv__’, ’__trunc__’, ’as_integer_ratio’, ’conjugate’, ’fromhex’, ’hex’, ’imag’, ’is_integer’, ’real’]》》》 》》》 class A:
x=3
y=4》》》 class B(A):
z=5》》》 dir(B)  # 类[’__doc__’, ’__module__’, ’x’, ’y’, ’z’]》》》 123456789101112131415161718

• Otherwise, the list contains the object’s attributes’ names, the names of its class’s attributes, and recursively of the attributes of its class’s base classes.

3、_ doc_

在Python中有一个奇妙的特性,文档字符串,又称为DocStrings。 
用它可以为我们的模块、类、函数等添加说明性的文字,使程序易读易懂,更重要的是可以通过Python自带的标准方法将这些描述性文字信息输出。 
上面提到的自带的标准方法就是_ doc _。前后各两个下划线。 
注:当不是函数、方法、模块等调用doc时,而是具体对象调用时,会显示此对象从属的类型的构造函数的文档字符串。

》》》 import math》》》 math.__doc__   # 模块’This module is always available.  It provides access to the\nmathematical functions defined by the C standard.’》》》 abs.__doc__   # 内置函数’abs(number) -》 number\n\nReturn the absolute value of the argument.’》》》 def addxy(x,y):
’’’the sum of x and y’’’
return x+y》》》 addxy.__doc__  # 自定义函数’the sum of x and y’》》》 a=[1,2,4]》》》 a.count.__doc__  # 方法’L.count(value) -》 integer -- return number of occurrences of value’》》》 b=3》》》 b.__doc__   # 具体的对象“int(x=0) -》 int or long\nint(x, base=10) -》 int or long\n\nConvert a number or string to an integer, or return 0 if no arguments\nare given.  If x is floating point, the conversion truncates towards zero.\nIf x is outside the integer range, the function returns a long instead.\n\nIf x is not a number or if base is given, then x must be a string or\nUnicode object representing an integer literal in the given base.  The\nliteral can be preceded by ’+’ or ’-’ and be surrounded by whitespace.\nThe base defaults to 10.  Valid bases are 0 and 2-36.  Base 0 means to\ninterpret the base from the string as an integer literal.\n》》》 int(’0b100’, base=0)\n4“》》》 12345678910111213141516171819

其实我们可以通过一定的手段来查看这些文档字符串,比如使用Pycharm,在对应的模块、函数、方法等上鼠标“右击”-》Go to-》Declaration。例如:查看内置函数abs的文档字符串 
 
我们再举一个具体的对象的例子,例如,上面具体的整型对象b的doc显示的就是其所从属的int类型的文档字符串: 

参考文献: 
1、Python帮助文档


python dir 和something 的class有什么关系


没有something这个东西。
Python下一切皆对象,每个对象都有多个属性(attribute),python对属性有一套统一的管理方案。
__dict__与dir()的区别:
dir()是一个函数,返回的是list;
__dict__是一个字典,键为属性名,值为属性值;
dir()用来寻找一个对象的所有属性,包括__dict__中的属性,__dict__是dir()的子集;
并不是所有对象都拥有__dict__属性。许多内建类型就没有__dict__属性,如list,此时就需要用dir()来列出对象的所有属性。
__dict__属性
__dict__是用来存储对象属性的一个字典,其键为属性名,值为属性的值。
#!/usr/bin/python
# -*- coding: utf-8 -*-
class A(object):
class_var = 1
def __init__(self):
self.name = ’xy’
self.age = 2
@property
def num(self):
return self.age + 10
def fun(self):pass
def static_f():pass
def class_f(cls):pass
if __name__ == ’__main__’:#主程序
a = A()
print a.__dict__ #{’age’: 2, ’name’: ’xy’} 实例中的__dict__属性
print A.__dict__
’’’
类A的__dict__属性
{
’__dict__’: 《attribute ’__dict__’ of ’A’ objects》, #这里如果想深究的话查看参考链接5
’__module__’: ’__main__’, #所处模块
’num’: 《property object》, #特性对象
’class_f’: 《function class_f》, #类方法
’static_f’: 《function static_f》, #静态方法
’class_var’: 1, ’fun’: 《function fun 》, #类变量
’__weakref__’: 《attribute ’__weakref__’ of ’A’ objects》,
’__doc__’: None, #class说明字符串
’__init__’: 《function __init__ at 0x0000000003451AC8》}
’’’
a.level1 = 3
a.fun = lambda :x
print a.__dict__ #{’level1’: 3, ’age’: 2, ’name’: ’xy’,’fun’: 《function 《lambda》 at 0x》}
print A.__dict__ #与上述结果相同
A.level2 = 4
print a.__dict__ #{’level1’: 3, ’age’: 2, ’name’: ’xy’}
print A.__dict__ #增加了level2属性
print object.__dict__
’’’
{’__setattr__’: 《slot wrapper ’__setattr__’ of ’object’ objects》,
’__reduce_ex__’: 《method ’__reduce_ex__’ of ’object’ objects》,
’__new__’: 《built-in method __new__ of type object at》,
等.....
’’’12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849501234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
从上述代码可知,
实例的__dict__仅存储与该实例相关的实例属性,
正是因为实例的__dict__属性,每个实例的实例属性才会互不影响。
类的__dict__存储所有实例共享的变量和函数(类属性,方法等),类的__dict__并不包含其父类的属性。
dir()函数
dir()是Python提供的一个API函数,dir()函数会自动寻找一个对象的所有属性(包括从父类中继承的属性)。
一个实例的__dict__属性仅仅是那个实例的实例属性的集合,并不包含该实例的所有有效属性。所以如果想获取一个对象所有有效属性,应使用dir()。
print dir(A)
’’’
[’__class__’, ’__delattr__’, ’__dict__’, ’__doc__’, ’__format__’, ’__getattribute__’, ’__hash__’, ’__init__’, ’__module__’, ’__new__’, ’__reduce__’, ’__reduce_ex__’, ’__repr__’, ’__setattr__’, ’__sizeof__’, ’__str__’, ’__subclasshook__’, ’__weakref__’, ’age’, ’class_f’, ’class_var’, ’fun’, ’level1’, ’level2’, ’name’, ’num’, ’static_f’]
’’’
a_dict = a.__dict__.keys()
A_dict = A.__dict__.keys()
object_dict = object.__dict__.keys()
print a_dict
print A_dict
print object_dict
’’’
[’fun’, ’level1’, ’age’, ’name’]
[’__module__’, ’level2’, ’num’, ’static_f’, ’__dict__’, ’__weakref__’, ’__init__’, ’class_f’, ’class_var’, ’fun’, ’__doc__’]
[’__setattr__’, ’__reduce_ex__’, ’__new__’, ’__reduce__’, ’__str__’, ’__format__’, ’__getattribute__’, ’__class__’, ’__delattr__’, ’__subclasshook__’, ’__repr__’, ’__hash__’, ’__sizeof__’, ’__doc__’, ’__init__’]
’’’
#因为每个类都有一个__doc__属性,所以需要去重,去重后然后比较
print set(dir(a)) == set(a_dict + A_dict + object_dict) #True12345678910111213141516171819201234567891011121314151617181920
结论
dir()函数会自动寻找一个对象的所有属性,包括__dict__中的属性。
__dict__是dir()的子集,dir()包含__dict__中的属性。

Python中处理属性的重要属性和函数是什么


处理属性的重要属性和函数

1、特殊属性

__class__:对象所属类的引用(即obj.__class__和type(obj)的作用相同)。Python中的某些特殊方法比如 __getattr__,只在对象的类中寻找,而不在实例中寻找。__dict__:一个映射,存储对象或类的可写属性。__slots__:类可以定义这个属性,限制实例有哪些属性。

2、内置函数

dir([object]):列出对象的大多数属性。getattr(object,name[,default]):从object对象中获取name字符串对应的属性。获取的属性可能来自对象所属的类或超类。hasattr(object,name):若object对象中存在指定的属性,或者能以某种方式(如继承)通过object对象获取指定的属性,返回True。setattr(object,name,value):把object对象指定属性的值设为value,前提是object对象能接受那个值。这个函数可能会创建一个新属性,或者覆盖现有的属性。var([object]):返回object对象的__dict__属性。

相关推荐:《Python视频教程》

3、特殊方法

__delattr__(self,name):只要使用del语句删除属性,就会调用这个方法。__dir__(self):把对象传给dir函数时调用,列出属性。__getattr__(self,name):仅当获取指定的属性失败,搜索过obj,Class和超类之后调用。__getattribute__(self,name):尝试获取指定的属性时总会调用这个方法。不过寻找的属性是特殊属性或特殊方法时除外。为了防止无限递归,__getattribute__方法的实现要使用super().__getattribute__(obj,name)。__setattr__(self,name,value):尝试设置指定的属性时总会调用这个方法。点号和setattr内置函数会触发这个方法。

相关推荐:

Python中的属性和特性是什么


python的dir和help用法


dir和help是Python中两个强大的built-in函数,就像Linux的man一样,绝对是开发的好帮手。比如查看list的所以属性:
dir(list)
输出:
[’__add__’, ’__class__’, ’__contains__’, ’__delattr__’, ’__delitem__’, ’__delslice__’, ’__doc__’, ’__eq__’, ’__format__’, ’__ge__’, ’__getattribute__’, ’__getitem__’, ’__getslice__’, ’__gt__’, ’__hash__’, ’__iadd__’, ’__imul__’, ’__init__’, ’__iter__’, ’__le__’, ’__len__’, ’__lt__’, ’__mul__’, ’__ne__’, ’__new__’, ’__reduce__’, ’__reduce_ex__’, ’__repr__’, ’__reversed__’, ’__rmul__’, ’__setattr__’, ’__setitem__’, ’__setslice__’, ’__sizeof__’, ’__str__’, ’__subclasshook__’, ’append’, ’count’, ’extend’, ’index’, ’insert’, ’pop’, ’remove’, ’reverse’, ’sort’]
然后查看list的pop方法的作用和用法:
help(list.pop)
输出:
Help on method_descriptor:
pop(...)
    L.pop([index]) -》 item -- remove and return item at index (default last).
    Raises IndexError if list is empty or index is out of range.
(END)

如何在python函数内部获得全局的dir


查官方文档呀
dir方法是针对类的,类写了__dir__()才能通过dir获取,python大部分数据类型都是类所以能调用,全局似乎不是。

python语言中的内建函数dir()是干啥用的啊


dir() 函数
尽管查找和导入模块相对容易,但要记住每个模块包含什么却不是这么简单。您并不希望总是必须查看源代码来找出答案。幸运的是,Python 提供了一种方法,可以使用内置的 dir() 函数来检查模块(以及其它对象)的内容。
dir() 函数可能是 Python 自省机制中最著名的部分了。它返回传递给它的任何对象的属性名称经过排序的列表。如果不指定对象,则 dir() 返回当前作用域中的名称

推荐阅读

    excel怎么用乘法函数

    excel怎么用乘法函数,乘法,函数,哪个,excel乘法函数怎么用?1、首先用鼠标选中要计算的单元格。2、然后选中单元格后点击左上方工具栏的fx公

    excel中乘法函数是什么?

    excel中乘法函数是什么?,乘法,函数,什么,打开表格,在C1单元格中输入“=A1*B1”乘法公式。以此类推到多个单元。1、A1*B1=C1的Excel乘法公式

    标准差excel用什么函数?

    标准差excel用什么函数?,函数,标准,什么,在数据单元格的下方输入l标准差公式函数公式“=STDEVPA(C2:C6)”。按下回车,求出标准公差值。详细

    扬声器属性级别设置|扬声器属性高级

    扬声器属性级别设置|扬声器属性高级,,1. 扬声器属性高级选择“高级”标签试试,不行的话,说明系统有问题了,直接换个验证过的系统盘重装系统就

    excel常用函数都有哪些?

    excel常用函数都有哪些?,函数,哪些,常用,1、SUM函数:SUM函数的作用是求和。函数公式为=sum()例如:统计一个单元格区域:=sum(A1:A10)  统计多个