私はクラス分数を作成するはずですが、すべての関数は「魔法」です。問題は私の教授が私たちに魔法を教えてくれたことではありません。私は使用しました分数クラスpython
__init__
他の魔法の方法を使用したことはありません。私はこれをプログラムするのに2日間持っており、私はこのコードを構築するためのガイダンスが必要です。
https://gist.github.com/mustaa/2350807 しかし、私はそのコードで何が起こっているのか理解できない
魔法のメソッドは次のとおりです:class Fraction: #I need it in a form like the one on github, but easier to understand
__init__ #construct a rational number with a given numerator and denominator
__add__ #add two fractions
__sub__ #subtract to fractions
__eq__ #check if 2 fractions are equal
__ne__ #check if 2 are not equal
__lt__ #check if one fraction is less than the other
__le__ #check if <=
__gt__ #check if one fraction is greater than the other
__ge__ #check if >=
__float__ #gets float representation of fraction called by float()
__repr__ #gets a string representation of the Fraction instance, called by str()
私はのためのスケルトンコードを取得したら、私はここで見つける私はプログラミングするために必要なものに近いです:
class Fraction, __add__, __eq__
私は残りの課題を自分自身で終えることができます。
私はあなたの質問からあなたの課題のためにあなたが配達することになっているか分かりません。 – roganjosh
これらはすべて[公式ドキュメント](https://docs.python.org/3/reference/datamodel.html#special-method-names)で説明されています – UnholySheep
Pythonの公式の 'Fraction'ファクトリ関数のコードはすべて開いていますあなたがIPython/Jupyterを使用している場合は、 'from fractions import Fraction'を実行し、' Fraction ?? 'を実行します(2つの疑問符でソースコードが表示されます)。 –