tutorialsPythonBasic.basic.classes.class_special_methods module

An example using some of the Python special class methods.
http://docs.python.org/2/reference/datamodel.html#basic-customization
class tutorialsPythonBasic.basic.classes.class_special_methods.Fraction(x, y)[source]

Bases: builtins.object

A simple fraction.

reduce()[source]

Reduce the fraction.

>>> frac = Fraction(2, 4)
>>> frac.reduce()
>>> print(frac)
1/2
>>> frac = Fraction(5, 8)
>>> frac.reduce()
>>> print(frac)
5/8
>>> frac = Fraction(-6, 9)
>>> frac.reduce()
>>> print(frac)
-2/3
value()[source]

Returns the value of the Fraction.

>>> Fraction(1, 2).value()
0.5
>>> Fraction(2, 4).value()
0.5
tutorialsPythonBasic.basic.classes.class_special_methods.gcd(a, b)[source]

Return the greatest common divisor of a and b.

>>> gcd(2, 10)
2
>>> gcd(6, 9)
3
>>> gcd(5, 22)
1