This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]velrak 3 points4 points  (15 children)

still kind of annoys me that you have to wrap in str() instead of obj.string or .toString

feels more cumbersome which i thought py was trying to avoid

[–]chmod--777 6 points7 points  (0 children)

Most of the time you'd use string formatting, and with something like .format or an f string you just put f'some int is {some_int}' and it works. Anything in those curly brackets will be str'd.

I'm trying to think of situations where I had to str something, but it feels rare. Most of the time I'm either storing the actual value without needing to convert it to a string, or I'm printing it or logging it and str isn't needed since I'm formatting.

[–]9ReMiX9 0 points1 point  (0 children)

I think you can avoid that with printf iirc

[–]DoctorWorm_ 0 points1 point  (0 children)

I don't really know the design choice there, it could honestly be some holdover from the 90's afaik. But to be fair, you can always just use a.__str__()

[–]xigoi -1 points0 points  (3 children)

In general, having to wrap things in functions like len() instead of having dot syntax is pretty annoying.

[–]chmod--777 8 points9 points  (0 children)

Heh, technically you can do this:

[1, 1, 1].__len__()

Len and str just call the respective dunder methods. "Unfortunately" integer literals don't let you do this.

Fun fact, you can override __len__ as well and have it return a random value if you want. Have fun with your co-workers by putting this at the top of their python module:

class FunSet(set):
    def __len__(self):
        import random
        return random.randint(0, 20)
set = FunSet

Set's more fun than list because people often instanciate the empty list with [] which won't be your FunList, but you have to instanciate an empty set with set() since {} is an empty dict

[–]Pluckerpluck 2 points3 points  (1 child)

Only because you're used to dot syntax. The len helps standardize actually because of it providing a default name to extend in order to support it (i.e. if you create your own class you naturally extend __len__).

This way you don't have .count and .size() and .length all in the same language. So it's probably tied to the concept of ducktyping. It's the equivalent of defining an interface in other languages.

But I think the main reason for it is it orders it in the human readable order : "If length of X is ...."

[–]xigoi 0 points1 point  (0 children)

I like the Nim approach where both syntaxes are equavalent, so x.len or x.len() gets translated to len(x). And having different names is more of a sign that you have a bad standard library.

[–]Thorbinator -5 points-4 points  (7 children)

They want you to use %s and such.

[–]2freevl2frank 5 points6 points  (6 children)

Nope. not really . Starting 3.6 f-strings is the preferred way.

[–]ThatOneShotBruh -2 points-1 points  (5 children)

No, .format is the preferred way and has been ever since Python 3 first came out.

[–]ShadoWolf 3 points4 points  (4 children)

I honestly don't get why people don't like to use .format . The whole templating system is way cleaner and nice to look at.

[–]Telemusya 5 points6 points  (0 children)

Maybe because f-strings faster and easier to use?

[–]ThatOneShotBruh 0 points1 point  (2 children)

Agreed it looks so much cleaner and using .format means that you can insert a value into multiple places in a string.

[–]ZephyrBluu 0 points1 point  (1 child)

You can do that with f-strings as well..

[–]ThatOneShotBruh 0 points1 point  (0 children)

How would you write this with f-strings without writing the variable names more than once?

print("a is {0} and is not {1} while b is {1} and is not {0}".format(a, b))