Recently I have been shifting from Python2.x to Python3.x. So, all notes below are only guaranteed in Python3.x.

1. all(iterable) and any(iterable)
They are equivalent to following functions respectively:

def all(iterable):
for element in iterable:
if not element:
return False
return True
def any(iterable):
for element in iterable:
if element:
return True
return False
2. bin(number) and hex(number)
Convert an integer number to a binary or hexadecimal string (starts with '0b' or '0x'). The result is a valid Python expression.

3. chr(i)
Return the string of one character whose Unicode codepoint is the integer i. For example, chr(97) returns the string 'a'. This is the inverse of ord().

4. classmethod
The @classmethod form is a function decorator. A class method receives the class as implicit first argument, just like an instance method receives the instance. To declare a class method, use this idiom:

class C:
@classmethod
def f(cls, arg1, arg2, ...): ...
It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class. If a class method is called for a derived class, the derived class object is passed as the implied first argument. Note that class methods are different than C++ or Java static methods.

5. 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.

6. dir([object])
Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object.

7. divmod(a, b)
Take two (non complex) numbers as arguments and return a pair of numbers consisting of their quotient and remainder.

8. enumerate(iterable[, start=0])
Return a tuple containing a count (from start which defaults to 0) and the corresponding value obtained from iterating over iterable.

9. eval(expression[, globals[, locals]]), exec(object[, globals[, locals]]), and compile(source, filename, mode[, flags[, dont_inherit]])
Here I just show some simple examples. For more details, please check here.

>>> glo = {'x':5, 'y':6}
>>> eval('x+y', glo)
11
>>> glo = {'aList':[1,2,3]}
>>> exec('for i in aList: print i', glo)
1
2
3
>>> aString = 'for i in range(3): print i'
>>> aStatement = compile(aString, '', 'exec')
>>> exec(aStatement)
0
1
2
Note:
a) The built-in functions globals() and locals() return the current global and local dictionary, respectively, which may be useful to pass around for use as the second and third argument to exec().
b) execfile function is no longer used in Python3.x. Instead of execfile(fn) use exec(open(fn).read()).

10. frozenset([iterable])
To represent sets of sets, the inner sets must be frozenset objects.

11. getattr(object, name[, default])
Return the value of the named attributed 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.

12. 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 exception or not.)

13. hash(object)
Return the hash value of the object (if it has one). Hash values are integers. They are used to quickly compare dictionary keys during a dictionary lookup. Numeric values that compare equal have the same hash value (even if they are of different types, as is the case for 1 and 1.0).

14. id(object)
Return the “identity” of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id value. (Implementation note: this is the address of the object.)

15. memoryview(obj)
Return a “memory view” object created from the given argument. For example:
>>> data = bytearray(b'abcefg')
>>> v = memoryview(data)
>>> v.readonly
False
>>> v[0] = 'z'
>>> data
bytearray(b'zbcefg')
>>> v[1:4] = b'123'
>>> data
bytearray(b'a123fg')
>>> v[2] = b'spam'
Traceback (most recent call last):
File "", line 1, in
ValueError: cannot modify size of memoryview object
There is a tolist() method which returns the data in the buffer as a list of integers:
>>> memoryview(b'abc').tolist()
[97, 98, 99]
16. 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.

17. staticmethod
A static method does not receive an implicit first argument. To declare a static method, use this idiom:
class C:
@staticmethod
def f(arg1, arg2, ...): ...
It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class.

18. vars([object])
Without arguments, return a dictionary corresponding to the current local symbol table. With a module, class or class instance object as argument (or anything else that has a __dict__ attribute), returns a dictionary corresponding to the object’s symbol table.

0 comments

Post a Comment