Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def isInteger(n):
- return int(n) == n
- isinstance(n, int)
- type(n) is int
- return int(n) == n
- def myintfunction(value):
- """ Please pass an integer """
- return 2 + value
- >>> myintfunction(5)
- 7
- >>> myintfunction('text')
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- File "<stdin>", line 3, in myintfunction
- TypeError: unsupported operand type(s) for +: 'int' and 'str'
- >>> print myintfunction(2.2)
- 4.2
- >>> print myintfunction(5j)
- (2+5j)
- >>> import decimal
- >>> myintfunction(decimal.Decimal('15'))
- Decimal("17")
- >>> class MyAdderClass(object):
- ... def __radd__(self, value):
- ... print 'got some value: ', value
- ... return 25
- ...
- >>> m = MyAdderClass()
- >>> print myintfunction(m)
- got some value: 2
- 25
- def myintfunction(value):
- """ Please pass an integer """
- return upstreamfunction(int(value))
- assert isinstance(...)
- assert type() is xxxx
- python -OO program.py
- class MyInt(int):
- # ... extra stuff ...
- int('sometext')
- <type 'exceptions.ValueError'>: invalid literal for int() with base 10: 'sometext'
- try:
- int(myvar) + 50
- except ValueError:
- print "Not a number"
- if type(n) is int
- def check_and_convert(x):
- x = int(x)
- assert 0 <= x <= 255, "must be between 0 and 255 (inclusive)"
- return x
- class IPv4(object):
- """IPv4 CIDR prefixes is A.B.C.D/E where A-D are
- integers in the range 0-255, and E is an int
- in the range 0-32."""
- def __init__(self, a, b, c, d, e=0):
- self.a = check_and_convert(a)
- self.b = check_and_convert(a)
- self.c = check_and_convert(a)
- self.d = check_and_convert(a)
- assert 0 <= x <= 32, "must be between 0 and 32 (inclusive)"
- self.e = int(e)
Add Comment
Please, Sign In to add comment