列出常见类型的方法:
def ListFunctions(lists):
print ( " ------------------------------------------ " )
print (type(lists))
for item in dir(lists):
if ( not item.startswith( " __ " )):
print (item)
# list
l = [ 1 , 2 , 3 ] # or list(1,2,3)
ListFunctions(l)
# tuple
t = ( 1 , 2 , 3 )
ListFunctions (t)
# set
s = { 1 , 2 , 3 } # or set(1,2,3)
ListFunctions(s)
# dict
d
=
{
1
:
'
1v
'
,
2
:
'
2v
'
,
3
:
'
3v
'
}
#
or dict(1:'1v', 2:'2v', 3:'3v')
ListFunctions(d)
# str
myStr = " 123 " # or str("123")
ListFunctions(myStr)
# file
file = open( " test\\file.txt " , " r " )
ListFunctions(file)
ListFunctions(d)
# str
myStr = " 123 " # or str("123")
ListFunctions(myStr)
# file
file = open( " test\\file.txt " , " r " )
ListFunctions(file)
运行结果:
------------------------------------------
< class ' list ' >
append
count
extend
index
insert
pop
remove
reverse
sort
------------------------------------------
< class ' tuple ' >
count
index
------------------------------------------
< class ' set ' >
add
clear
copy
difference
difference_update
discard
intersection
intersection_update
isdisjoint
issubset
issuperset
pop
remove
symmetric_difference
symmetric_difference_update
union
update
------------------------------------------
< class ' dict ' >
clear
copy
fromkeys
get
items
keys
pop
popitem
setdefault
update
values
------------------------------------------
< class ' str ' >
_formatter_field_name_split
_formatter_parser
capitalize
center
count
encode
endswith
expandtabs
find
format
index
isalnum
isalpha
isdecimal
isdigit
isidentifier
islower
isnumeric
isprintable
isspace
istitle
isupper
join
ljust
lower
lstrip
maketrans
partition
replace
rfind
rindex
rjust
rpartition
rsplit
rstrip
split
splitlines
startswith
strip
swapcase
title
translate
upper
zfill
------------------------------------------
< class ' _io.TextIOWrapper ' >
_CHUNK_SIZE
_checkClosed
_checkReadable
_checkSeekable
_checkWritable
buffer
close
closed
detach
encoding
errors
fileno
flush
isatty
line_buffering
name
newlines
read
readable
readline
readlines
seek
seekable
tell
truncate
writable
write
writelines
< class ' list ' >
append
count
extend
index
insert
pop
remove
reverse
sort
------------------------------------------
< class ' tuple ' >
count
index
------------------------------------------
< class ' set ' >
add
clear
copy
difference
difference_update
discard
intersection
intersection_update
isdisjoint
issubset
issuperset
pop
remove
symmetric_difference
symmetric_difference_update
union
update
------------------------------------------
< class ' dict ' >
clear
copy
fromkeys
get
items
keys
pop
popitem
setdefault
update
values
------------------------------------------
< class ' str ' >
_formatter_field_name_split
_formatter_parser
capitalize
center
count
encode
endswith
expandtabs
find
format
index
isalnum
isalpha
isdecimal
isdigit
isidentifier
islower
isnumeric
isprintable
isspace
istitle
isupper
join
ljust
lower
lstrip
maketrans
partition
replace
rfind
rindex
rjust
rpartition
rsplit
rstrip
split
splitlines
startswith
strip
swapcase
title
translate
upper
zfill
------------------------------------------
< class ' _io.TextIOWrapper ' >
_CHUNK_SIZE
_checkClosed
_checkReadable
_checkSeekable
_checkWritable
buffer
close
closed
detach
encoding
errors
fileno
flush
isatty
line_buffering
name
newlines
read
readable
readline
readlines
seek
seekable
tell
truncate
writable
write
writelines
注意:
open()函数返回的file object的类型取决与传入的mode。当open()使用(w,r,wt,rt)来打开文本文件时,它返回的是io.TextIOBase的子类型,特别地为io.TextIOWrapper。当使用buffering打开binary文件时,他返回的是io.BufferedIOBase的子类型,当为读时返回为io.BufferedReader,当为写或增加时返回为io.BufferedWriter,当为读写模式时,它返回的是io.BufferedRandom。当buffering没有指定时,返回的是io.rawiobase,io.fileio的子类型。
完!