5 ms·
Sorry I should have made things more clear. I am talking about in things like function signatures. Doing something like isinstance(x, str) is discouraged and c
by hahainternet 11y ago
Sorry I should have made things more clear. I am talking about in things like function signatures.
Doing something like isinstance(x, str) is discouraged and considered non-pythonic, and recently hasattr() has also been talked about. However, both strings and lists can be iterated, so assuming that this will fail is incorrect:
x = "Hello"
for y in x:
print("{} is {}".format(y, type(y)))
I asked on IRC and ultimately the only answer I got was that the most pythonic way to do things was to name functions whatever_str and whatever_list.
edit: I also should have said above "lists can't be compared with strings using <". I was trying to point out Python did things potentially correctly there, but I see now it's ambiguous.
- DonHopkins 11y agoIf it's so important to decorate variables with unique punctuation to visually distinguish between two particular kinds of many possible types, then why don't you also decorate function calls according to whether they return lists or scalars? How should you decorate functions that return different types depending on what context they're used in? http://perlmaven.com/wantarray http://perlmaven.com/wantarray If it's so important to visually distinguish the names of scalars and arrays, then shouldn't you also be able to distinguish a string from an int, like BASIC's a$ -vs- i%? So why just distinguish lists and scalars? Why not go all the way to Hungarian Punctuation and use Unicode and Emoji characters to uniquely decorate variables for every other possible built-in and user defined type? https://en.wikipedia.org/wiki/Hungarian_notation#Relation_to_sigils https://en.wikipedia.org/wiki/Hungarian_notation#Relation_to...
- hahainternet 11y agoYou might be a little confused, I'm pretty sure that wantarray and the concept of functions caring about context is entirely gone in Perl 6. Now you are expected to return objects which 'do the right thing'. However, if you really want to do some of the other things you've listed they are possible to do. Perl 6's syntax can be augmented by the programs being executed. I'm honestly interested though, was I ultimately wrong above? Is there a smart way to define a function in python that can take both a single item and a list of items? In Perl 6 I'd just define a multi sub where the list form applies the single item sub for each item in the list.
- DonHopkins 11y agoIt might be gone from Perl 6, but the idea of dividing all possible types into two different classes, and then using punctuation to distinguish variables holding those two classes, is from Perl 5, just like the idea of functions caring about the context of their return value. If Perl 6 is an entirely new language, then why is it riddled with so many bad ideas from another language that only coincidentally shares the first 4 alphabetic characters of its name? Why the artificial scalar/array dichotomy? What is it that is so important about being able to distinguish variables holding scalar-like values and array-like values by punctuation, but it doesn't matter that you can't distinguish variables holding strings and integers by their punctuation? And how can you be consistent about types that act like both scalars and arrays, like strings? Or user defined types that might behave like scalars or arrays but are represented by objects? Are those treated as arrays? Or are they references, treated as scalars? Scalar -vs- array is a false dichotomy that's deeply and unnecessarily embedded in the very syntax of the language.
- hahainternet 11y agoI'm not entirely sure you've illustrated that it's a bad idea. I believe that I illustrated that a simple pattern is not easily doable in Python but is in Perl 6. You'll have to show that this is a bad pattern or the implementation is bad or something at least to prove your point. There's more to sigils than just scalars and arrays too, as you can see here: http://doc.perl6.org/language/variables#Sigils http://doc.perl6.org/language/variables#Sigils The difference in your examples are that scalars describe more the 'shape' of the variable than its type. Given that a lot of programming is dealing with lists or 'dictionaries' this is a fairly minimal intrusion into the code that very much improves succinctness, correctness and the ability to easily read what some code is doing. Oh, and strings don't act like arrays in Perl 6 AFAIK. Because they could be split on characters, or codepoints, or bytes, you must be explicit: > for "Hello".comb -> $c { say "Character $c" } Character H Character e Character l Character l Character o (Or to be perly) > .say for "Hello".comb
- DonHopkins 11y ago
- muraiki 11y agoI downvoted you, but perhaps too hastily. I think that sigils are a matter of opinion, and there are benefits to using them and also to not using them. I think that this downvote was really intended for your original comment, which I interpreted as essentially stating a personal preference as fact.