R
rantingrick
============================================================
The "Fundamental Five" built-in functions
============================================================
There are quite a few helpful built in functions provided to the
python programmer however in my mind five of them are the most
important to Python noobs. The "fundamental five" i call them. I
believe you should not do anything with Python until you are
completely familier with these five because most of the bugs and mis-
understanding a new user experiences can be solved by using these five
functions.
============================================================
--------------------------------------------------
1. help()
--------------------------------------------------
http://docs.python.org/py3k/library/functions.html#help
The "help" function is by far the most important of all Python built
in functions. It is a gift handed down from the gods who reside at py-
dev so don't balk at it or you'll suffer plagues of exceptions. Unlike
other languages, Python has documentation built-in in the form of
docstrings and the help function will pull out as much or as little
info as you'd like from these do strings. No need to carry around
heavy books or hundreds of links to www tuts because everything you
need to know (reference wise) is available via the help function. Run
the help function and ye shall be enlightened!
--------------------------------------------------
2. dir()
--------------------------------------------------
http://docs.python.org/py3k/library/functions.html#dir
Python has name spaces. Name spaces are awesome. Name spaces keep code
from clashing with other code. The dir() function will list the
currently defined names in the current namespace. If your getting
NameErrors check the name space with dir() function.
--------------------------------------------------
3. repr()
--------------------------------------------------
http://docs.python.org/py3k/library/functions.html#repr
Most new user think that printing an object to stdout is all they'll
ever need. However when you call print -- or sys.stdout.write(object)
-- you are only seeing a "friendly" version of the object. You can
even control the return value in your own classes bu overriding the
obj.__str__() special method. This friendly version is sufficient most
of the time HOWEVER when debugging it can lead you to smash you head
on the keyboard repeatedly due to it's sometimes misleading nature. If
you need to get the true "representation" of an object then call...
repr(object).
--------------------------------------------------
4. type()
--------------------------------------------------
http://docs.python.org/py3k/library/functions.html#isinstance
Ever had a TypeError? Ever had some object you were just sure was one
type but is not behaving like that type? Then check it's type for
Pete's sake! Even experienced programmers (of many languages) suffer
from TypeErrors (be them subtle or not) because the type of an object
cannot be inferred simply by looking at it's identifier. So when
something ain't right, skip the tripe, and check the type!
--------------------------------------------------
5. id()
--------------------------------------------------
http://docs.python.org/py3k/library/functions.html#id
Ah yes, another source of frustration is the old "identical twin"
syndrome (or in the worst forms; multiplicity syndrome!). Yes, on the
surface that list of lists looks as if it contains unique elements
HOWEVER you keep getting strange results. How are you going to find
the problem? Hmm? Never fear my confused new friend, by comparing the
ids of two objects you can know if they are actually the same or
different. If the id is the same, the objects are the same.
--------------------------------------------------
Summary
--------------------------------------------------
These five functions are vital to debugging any python code or
understanding an API. You will be using these functions over and over
again, so the sooner you learn them the better!
==================================================
Extending "The Five"
==================================================
Once you have mastered the five it is time to expand your horizons a
bit. The next group of functions will complete the circle of
introspection.
==================================================
* hasattr()
* isintance()
* issubclass()
* callable()
* super()
* vars()
* globals()
* locals()
* ascii()
==================================================
General Built-in Functions by Group.
==================================================
--------------------------------------------------
Objects
--------------------------------------------------
Bulling an object:
* delattr()
* getattr()
* setattr()
Setting the rules:
* staticmethod()
* classmethod()
* property()
--------------------------------------------------
Numbers and Math
--------------------------------------------------
* abs()
* divmod()
* round()
* pow() # use ** or math.pow()
--------------------------------------------------
List / Collection Types:
--------------------------------------------------
* len()
* min()
* max()
* any()
* all()
* sum()
Ordering:
* sorted()
* slice() # Use list[start:stop].
* reversed()
* enumerate()
* zip()
Functional:
* filter()
* map()
Iteration:
* iter()
* next()
--------------------------------------------------
Input / Output:
--------------------------------------------------
* input()
* open()
* print()
* format()
--------------------------------------------------
Casting and Creating:
--------------------------------------------------
str(), int(), chr(), float(), oct(), ord(), complex(), hex(), bin(),
bool(),
tuple(), list(), range(), dict(), frozenset(), bytes(), bytearray(),
set(), hash(), memoryview(), object()
The "Fundamental Five" built-in functions
============================================================
There are quite a few helpful built in functions provided to the
python programmer however in my mind five of them are the most
important to Python noobs. The "fundamental five" i call them. I
believe you should not do anything with Python until you are
completely familier with these five because most of the bugs and mis-
understanding a new user experiences can be solved by using these five
functions.
============================================================
--------------------------------------------------
1. help()
--------------------------------------------------
http://docs.python.org/py3k/library/functions.html#help
The "help" function is by far the most important of all Python built
in functions. It is a gift handed down from the gods who reside at py-
dev so don't balk at it or you'll suffer plagues of exceptions. Unlike
other languages, Python has documentation built-in in the form of
docstrings and the help function will pull out as much or as little
info as you'd like from these do strings. No need to carry around
heavy books or hundreds of links to www tuts because everything you
need to know (reference wise) is available via the help function. Run
the help function and ye shall be enlightened!
--------------------------------------------------
2. dir()
--------------------------------------------------
http://docs.python.org/py3k/library/functions.html#dir
Python has name spaces. Name spaces are awesome. Name spaces keep code
from clashing with other code. The dir() function will list the
currently defined names in the current namespace. If your getting
NameErrors check the name space with dir() function.
--------------------------------------------------
3. repr()
--------------------------------------------------
http://docs.python.org/py3k/library/functions.html#repr
Most new user think that printing an object to stdout is all they'll
ever need. However when you call print -- or sys.stdout.write(object)
-- you are only seeing a "friendly" version of the object. You can
even control the return value in your own classes bu overriding the
obj.__str__() special method. This friendly version is sufficient most
of the time HOWEVER when debugging it can lead you to smash you head
on the keyboard repeatedly due to it's sometimes misleading nature. If
you need to get the true "representation" of an object then call...
repr(object).
--------------------------------------------------
4. type()
--------------------------------------------------
http://docs.python.org/py3k/library/functions.html#isinstance
Ever had a TypeError? Ever had some object you were just sure was one
type but is not behaving like that type? Then check it's type for
Pete's sake! Even experienced programmers (of many languages) suffer
from TypeErrors (be them subtle or not) because the type of an object
cannot be inferred simply by looking at it's identifier. So when
something ain't right, skip the tripe, and check the type!
--------------------------------------------------
5. id()
--------------------------------------------------
http://docs.python.org/py3k/library/functions.html#id
Ah yes, another source of frustration is the old "identical twin"
syndrome (or in the worst forms; multiplicity syndrome!). Yes, on the
surface that list of lists looks as if it contains unique elements
HOWEVER you keep getting strange results. How are you going to find
the problem? Hmm? Never fear my confused new friend, by comparing the
ids of two objects you can know if they are actually the same or
different. If the id is the same, the objects are the same.
--------------------------------------------------
Summary
--------------------------------------------------
These five functions are vital to debugging any python code or
understanding an API. You will be using these functions over and over
again, so the sooner you learn them the better!
==================================================
Extending "The Five"
==================================================
Once you have mastered the five it is time to expand your horizons a
bit. The next group of functions will complete the circle of
introspection.
==================================================
* hasattr()
* isintance()
* issubclass()
* callable()
* super()
* vars()
* globals()
* locals()
* ascii()
==================================================
General Built-in Functions by Group.
==================================================
--------------------------------------------------
Objects
--------------------------------------------------
Bulling an object:
* delattr()
* getattr()
* setattr()
Setting the rules:
* staticmethod()
* classmethod()
* property()
--------------------------------------------------
Numbers and Math
--------------------------------------------------
* abs()
* divmod()
* round()
* pow() # use ** or math.pow()
--------------------------------------------------
List / Collection Types:
--------------------------------------------------
* len()
* min()
* max()
* any()
* all()
* sum()
Ordering:
* sorted()
* slice() # Use list[start:stop].
* reversed()
* enumerate()
* zip()
Functional:
* filter()
* map()
Iteration:
* iter()
* next()
--------------------------------------------------
Input / Output:
--------------------------------------------------
* input()
* open()
* print()
* format()
--------------------------------------------------
Casting and Creating:
--------------------------------------------------
str(), int(), chr(), float(), oct(), ord(), complex(), hex(), bin(),
bool(),
tuple(), list(), range(), dict(), frozenset(), bytes(), bytearray(),
set(), hash(), memoryview(), object()