62
slicing arrays (hexbear.net)
submitted 4 months ago by Owl@hexbear.net to c/technology@hexbear.net
top 21 comments
sorted by: hot top controversial new old
[-] comrade_pibb@hexbear.net 10 points 4 months ago
[-] someone@hexbear.net 10 points 4 months ago

I remain baffled as to how the javascript-heavy modern web actually functions as well as it does, with all that dynamic insanity. Typescript isn't a miracle cure.

[-] hexaflexagonbear@hexbear.net 7 points 4 months ago* (last edited 4 months ago)

Everytime I have to make a small python tool available to my teammembers it really drives home how crazy it is because everything is realistically one of one or two types, and have to document it as such and build in a thousand little statements to make sure other people don't feed in the wrong type. What's madening is that how python does type checking changes every so often, and libraries are sometimes incosistent with how they name their types when borrowing classes from other libraries.

[-] invalidusernamelol@hexbear.net 4 points 4 months ago* (last edited 4 months ago)

It's funny how Python spent the past like 6 years developing the type hinting system (which is a massive improvement from no typing don't get me wrong) only to start re-introducing duck typing again with the Protocols.

The one thing I'll always love about it is the ability to override builtins and do dumb things like call 'sum(cats)' where cats is a list of cat objects and have it like print out meow to the terminal for each cat in the list.

Shit like that makes it perfect for internal tools.

[-] hexaflexagonbear@hexbear.net 2 points 4 months ago* (last edited 4 months ago)

At some point I'm just gonna give up and do

Try:

Entire fucking program

Except:

Print("error: something went wrong")

[-] invalidusernamelol@hexbear.net 4 points 4 months ago* (last edited 4 months ago)

Even better is to create a decorator and just wrap the offending functions:

def shut_up(func):
    def call(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except Exception as e:
            print(f"shit's fucked, but I'll be quiet about it")
            return
    return call

@shut_up
def add(x: int, y: int):
    print(x + y)

add(1, 2)
add(-1, 2)
add(1, "2")

>>> 3
>>> 1
>>> "shit's fucked, but I'll be quiet about it"

Or if you want to attempt to salvage it:

def shut_up(func):
    def call(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except Exception as e:
            try:
                return func(*map(int, args), **kwargs)
            except Exception as e:
                print(f"shit's really fucked, I even tried to fix it for you")
                return None
    return call
[-] edge@hexbear.net 6 points 4 months ago

Those are in no way incompatible.

[-] comrade_pibb@hexbear.net 3 points 4 months ago

Of course not, the syntax is making the type system obvious tho

[-] invalidusernamelol@hexbear.net 3 points 4 months ago* (last edited 4 months ago)

You just have to do it manually in Python or rely on systems like mypy to get 'static' checking.

It can be useful to rely on duck typing though if you don't want to have to re-implement or inherit from an existing base class to use a higer order function.

If the function is written in a way where all it needs is specific methods or parameters from its input objects, you can really save on interface bloat.

But if someone is used to writing statically typed code and has to deal with that it can create a lot of confusion and I always end up writing a ton of override signatures either in my code or in a .pyi sidecar so whoever is using it can still get some form of feedback that what they're doing is okay. Or at the very least putting that information in the docstring so people can read the docs as they're using the function.

[-] bumpusoot@hexbear.net 5 points 4 months ago

Static typing is for wusses who can't handle dynamic, go-getter variables pineapple-surf

[-] gammarays@lemm.ee 5 points 4 months ago

static typing:

  • afraid to make mistakes
  • restricts your freedom
  • thinks i'm too stupid to know the types of my variables
  • years of programming yet no real world use found for Option<Option<Result>>

dynamic typing

  • bold, fearless
  • null pointer exceptions? catch them and return 0
  • can use eval, easily extendable
[-] Owl@hexbear.net 3 points 4 months ago

I don't like static typing, it hurts my fingers, and I'm always worried it'll fry my motherboard.

[-] bumpusoot@hexbear.net 5 points 4 months ago

Of course, in Python, you can slice with [start:end:step], so it is valid to do list[:3:3] for double the happiness. Not that you'd ever practically do so.

[-] invalidusernamelol@hexbear.net 2 points 4 months ago* (last edited 4 months ago)

It's functionally identical to list[0] so you could definitely just refactor your code to use list[:3:3]

[-] da_gay_pussy_eatah@hexbear.net 2 points 4 months ago

I don't think that's equivalent. I think the former would return e0 and the latter would return [e0]

[-] invalidusernamelol@hexbear.net 1 points 4 months ago* (last edited 4 months ago)

You're right, it would return a list containing the first element of the sliced list.

So:

lst[:3:3] == [lst[0]]

Well, technically a sequence containing the first element of whatever sequence you sliced. Take advantage of implementing magic methods and you too can abuse slice notation with your classes.

[-] Tomorrow_Farewell@hexbear.net 4 points 4 months ago* (last edited 4 months ago)

you like slicing arrays, don't you?

Proceeds to slice a list. (EDIT: as opposed to an array.)

[-] invalidusernamelol@hexbear.net 4 points 4 months ago
c, b, a = ('unpacking', 'prefer', 'I')
print(a, b, c)
[-] Owl@hexbear.net 2 points 4 months ago

c, b, a =

chess battle advanced

[-] ChaosMaterialist@hexbear.net 3 points 4 months ago
[-] radiofreeval@hexbear.net 1 points 4 months ago
this post was submitted on 13 Jul 2024
62 points (98.4% liked)

technology

23313 readers
151 users here now

On the road to fully automated luxury gay space communism.

Spreading Linux propaganda since 2020

Rules:

founded 4 years ago
MODERATORS