In the following example, the same function is called with an integer, a floating point value, and a string.
def function(value):
print value
if __name__=="__main__":
function(1)
function(1.0)
function("one")
The Type function allows you to check what type a variable has. This funcation returns a type descriptor, which is unique object for each type provided by python interpreter
def dump(value):
print type(value),value
if __name__=="__main__":
dump(1)
dump(1.0)
dump("one")
>>>
<type ‘int’> 1
<type ‘float’> 1.0
<type ‘str’> one
Each type has a single corresponding type object, which means that you can use the is operator to do type testing like.
if isinstance(file, type(" "):
__ code__
The callable function checks if an object can be called.It returns true for functions, methos, lambda expression, classes, and class instance that define the __call__ method.
if callable(function):
print funcation, "is callable"
else:
print function, "is not callable"
You can use the isinstance function which checks if an object is an instance of a given classs like
if isinstance(object,classname):
The translate method of strings is quite powerful and flexible. A little factory function, returning a closure, can do wonders for this kind of task
import string
def translator(frm='',to='',delete='',keep=None):
if len(to)==1:
to = to * len(frm)
trans = string.maketrans(frm,to)
if keep is not None:
allchars = string.maketrans('','')
delete = allchars.translate(allchars,keep.translate(allchars,delete))
def translate(s):
return s.translate(trans,delete)
return translate
A simple and fast way to check whether something is a string or Unicode object is to use the built-ins isinstance and basestring, as follows
def isString(anObj):
return isinstance(anObj,basestring)
This is it! No more cricket this season now. It’s been really nice season for me and my team since last winter after defeat from EDF energy. We come back with stronger side with the help of various Net practices at Ovel, Regent park and at Holland park. But now it’s time for me to say “tata” to Cricket. I think I ended up minor injury at my hips and ball socket joint probably muscle stretch out. So I have decided not to play for this season. After all Ramadan is starting next week and I couldn’t think to play while I am fasting and by the way I have to concentrate on my career now.
It’s time for me to say “tata” to all sporting activities I do and Just try to impore my python skill.