Return an enumerate object.
The enumerate object yields pairs containing a count (from start, which defaults to zero) and a value yielded by the iterable argument.
enumerate is useful for obtaining an indexed list:
(0, seq[0]), (1, seq[1]), (2, seq[2]), …
enumerate(iterable, start=0)
Return an enumerate object. iterable must be a sequence, an iterator, or some other object which supports iteration. The next() method of the iterator returned by enumerate() returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over iterable.
enumerate函数返回一个enumerate对象(本质是一个迭代器),对iterable进行编号,迭代器的next方法每次返回一个元组,第一个元素为编号,第二个元素为相应的iterable元素。下例
b='shit'
c=enumerate({1,8,5,3})
print(c)
print(list(c))
print(tuple(c))
#result
<enumerate object at 0x000001EFAA6E38B8>
[(0, 8), (1, 1), (2, 3), (3, 5)]
()
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 to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.
getattr(object, name[, default]) -> value
Get a named attribute from an object; getattr(x, ‘y’) is equivalent to x.y.
When a default argument is given, it is returned when the attribute doesn’t
exist; without it, an exception is raised in that case.
getattr(x, y, default)函数输入一个对象和字符串,来返回该字符串对应的方法名或数据属性,相当于执行x.y ;如方法或属性不存在则报错,如给定了default值,不存在则返回default值;注意, 参数不能通过关键字方法给定,否则报错.
此函数配合hasattr可用作多人开发时的可插拔设置(当某个功能还未完成时,后续无法进行调用,但可用hasattr作判断).
函数示例:
class Teacher:
name = 'peter'
special = 'ass'
age = 88
__bitch = 'rock'
def __init__(self, name1, gender, age, special):
self.name1 = name1
self.gender = gender
self.age = age
self.special = special
print('i am teacher')
def running(self):
print('fucking teacher')
print(getattr(Teacher, 'name', 123))
print(getattr(S5, 'runningg', 123))
#result
peter
123
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.)
hasattr()函数输入一个对象和字符串,来判断字符串对应的方法是否适用于对象,是则返回True, 否则返回False; 其内部实现是通过调用getattr来实现的.
print(hasattr(Teacher, 'running'))
#result
True
setattr(object, name, value)
This is the counterpart of getattr(). The arguments are an object, a string and an arbitrary value. The string may name an existing attribute or a new attribute. The function assigns the value to the attribute, provided the object allows it. For example, setattr(x, ‘foobar’, 123) is equivalent to x.foobar = 123.
setattr(x, y, z)函数通过输入一个对象,属性key以及属性value来新增或修改对象属性,相当于x.y = z, 返回None
下例:
class S5(S2, S4):
def __init__(self):
print('i am s5')
print('s5 fuck')
fk2 = S5()
print(setattr(S5, 'walking', 'slow'))
print(fk2.walking)
#result
s5 fuck
i am s5
None
slow
delattr(object, name)
This is a relative of setattr(). The arguments are an object and a string. The string must be the name of one of the object’s attributes. The function deletes the named attribute, provided the object allows it. For example, delattr(x, ‘foobar’) is equivalent to del x.foobar.
delattr(a,b) 函数输入一个对象和字符串, 从对象的属性字典中删除该字符串对应的属性或方法,返回None, 只能删除自己的属性, 无法删除父类或子类的属性,不存在则报错;相当于del a.b
下例:
class S5(S2, S4):
def __init__(self):
print('i am s5')
print('s5 fuck')
fk2 = S5()
print(setattr(S5, 'walking', 'slow'))
print(setattr(fk2, 'age', 34))
print(fk2.__dict__)
print(delattr(fk2, 'age'))
print(fk2.__dict__)
#result
s5 fuck
i am s5
None
None
{'age': 34}
None
{}
注意:以上4种attr方法适用于所有对象(包括类)
print(*objects, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)
1)Print objects to the text stream file, separated by sep and followed by end. sep, end, file and flush, if present, must be given as keyword arguments.
2)All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.
3)The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used. Since printed arguments are converted to text strings, print() cannot be used with binary mode file objects. For these, use file.write(…) instead.
4)Whether output is buffered is usually determined by file, but if the flush keyword argument is true, the stream is forcibly flushed.
print()函数按照文本流的方式打印对象;参数sep(分隔字符,默认为空格),end(结束字符,默认为换行符),file(文件写入方法),flush(刷新,如果开启刷新,则读入一个就打印一个,关闭刷新则缓冲好后一起打印输出)必须以关键字的方式指定;如果不输入打印内容,则默认打印end参数规定的字符串.