1.python语言中的内建函数dir()是干啥用的啊
dir(。)
dir([object]) -> list of strings
Return an alphabetized list of names comprising (some of) the attributes
of the given object, and of attributes reachable from it:
No argument: the names in the current scope.
Module object: the module attributes.
Type or class object: its attributes, and recursively the attributes of
its bases.
Otherwise: its attributes, its class's attributes, and recursively the
attributes of its class's base classes.
2.python的dir和help用法
这个你可以去搜索官方文档, 有很详细的说明, 在" 3. Data model "中.
主要是用来将对象模拟成数值对象, 给他提供一些基于运算符的操作.
比如字符串"abc", 本来就字符串而言不具有对加法这种运算的处理, 但是python中给他定义了__add__, 然后在遇到类似"abc" + "def"这样的加法的时候, python就会去调用函数__add__来进行处理.
加减乘除, 异或, 模, 自加等等都有对应的__xx__函数
3.python的dir和help用法
当你给dir()提供一个模块名字时,它返回在那个模块中定义的名字的列表。
当没有为其提供参数时, 它返回当前模块中定义的名字的列表。dir() 函数使用举例: >>> import sys # 获得属性列表,在这里是sys模块的属性列表>>> dir(sys)['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_compact_freelists','_current_frames', '_getframe', 'api_version', 'argv', 。
] 如果您需要快速获取任何的Python函数或语句的信息,那么您可以使用内置的“help”(帮助)功能。这是非常有用的,尤其是当使用翻译提示符时,例如,运行‘help(print)”——这将显示print函数的帮助--用于打印东西到屏幕上。
help()函数使用举例: >>> help(print)Help on built-in function print in module builtins:print(。) print(value, 。
, sep=' ', end='\n', file=sys.stdout, flush=False)。
4.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)
5.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()则打印属性与属性的值。
6.python shell是什么东西
python shell是Python的命令行。
shell中最常用的是ls命令,python对应的写法是:os.listdir(dirname),这个函数返回字符串列表,里面是所有的文件名,不过不包含”.”和”..”。
如果要遍历整个目录的话就会比较复杂一点,在解释器里试一下:
>>> os.listdir(”/”)
['tmp', 'misc', 'opt', 'root', '.autorelabel', 'sbin', 'srv','.autofsck', 'mnt', 'usr', 'var', 'etc', 'selinux', 'lib', 'net','lost+found', 'sys', 'media', 'dev', 'proc', 'boot', 'home', 'bin']
就像这样,接下去所有命令都可以在python的解释器里直接运行观看结果。
扩展资料:
python shell对应于shutil.copy(src,dest),这个函数有两个参数,参数src是指源文件的名字,参数dest则是目标文件或者目标目录的名字。
如果dest是一个目录名,就会在那个目录下创建一个相同名字的文件。与shutil.copy函数相类似的是shutil.copy2(src,dest),不过copy2还会复制最后存取时间和最后更新时间。
不过,shell的cp命令还可以复制目录,python的shutil.copy却不行,第一个参数只能是一个文件。
其实,python还有个shutil.copytree(src,dst[,symlinks])。参数多了一个symlinks,它是一个布尔值,如果是True的话就创建符号链接。
移动或者重命名文件和目录,shutil.move(src,dst),与mv命令类似,如果src和dst在同一个文件系统上,shutil.move只是简单改一下名字,如果src和dst在不同的文件系统上,shutil.move会先把src复制到dst,然后删除src文件。
参考资料:Python—百度百科
转载请注明出处代码入门网 » dir()python