Deutsch English Français Italiano |
<l620pbFn16lU4@mid.individual.net> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news.mixmin.net!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: rbowman <bowman@montana.com> Newsgroups: comp.os.linux.advocacy Subject: Re: Why Python When There Is Perl? Date: 21 Mar 2024 06:01:16 GMT Lines: 42 Message-ID: <l620pbFn16lU4@mid.individual.net> References: <17be420c4f90bfc7$63225$1585792$802601b3@news.usenetexpress.com> <utd86u$1ipcj$1@solani.org> <17be75acfaf8f0f4$2017$3384359$802601b3@news.usenetexpress.com> <utfsvk$1pkkk$2@dont-email.me> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: individual.net qB7cDlBx8NOW1of7Dk9A7wM73IHi33cmwK01x4sd5lnOGiqCu3 Cancel-Lock: sha1:QMXk7MUN+Itdo03eFf5O8sDa0ZM= sha256:g7TaU4YJ/SnDuHz2bCuDBVR06sUsPEVDgeux0GXiwIw= User-Agent: Pan/0.149 (Bellevue; 4c157ba) Bytes: 2387 On Wed, 20 Mar 2024 19:54:29 -0400, DFS wrote: > Just learn Python and C side-by-side. Write a routine in one and > duplicate it in the other. It's enjoyable and educational. Side by side Python and C# is educational too. The initial implementation as in Python, a simple query of the iTunes API. I grabbed the artist, track, and time and put them in a class. Sorting was awkward since I didn't want the time to be used. When I moved to C# I created a similar class using the IEquitable and IComparable interfaces that allowed me to define Equals and CompareTo to only take into account some of the instance attributes. Going back to Python, how could I do the same. The answer was dunders. class Item: def __init__(self, artist, track, time): self.artist = artist self.track = track self.time = time def __eq__(self, other): if ( self.artist.casefold() == other.artist.casefold() and self.track.casefold() == other.track.casefold() ): return True else: return False def __lt__(self, other): return self.artist < other.artist or self.track < other.track def __gt__(self, other): return self.artist > other.artist or self.track > other.track So Python allowed me to rapidly develop the basic algorithm that I could easily implement in C#. Knowing C# a little better I had something to bring back to learn a new trick in Python.