Sujet : Re: Why Python When There Is Perl?
De : bowman (at) *nospam* montana.com (rbowman)
Groupes : comp.os.linux.advocacyDate : 21. Mar 2024, 08:01:16
Autres entêtes
Message-ID : <l620pbFn16lU4@mid.individual.net>
References : 1 2 3 4
User-Agent : Pan/0.149 (Bellevue; 4c157ba)
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.