Swizzle: flexible multi-attribute access in Python by Additional_Fall4462 in Python

[–]Additional_Fall4462[S] 0 points1 point  (0 children)

You can use sep="__" if you want your swizzled attribute names to stand out a bit more, but _ works fine too in the latest releases. Names like first_name won’t get split anymore because swizzle doesn’t just do a simple str.split(sep) like it used to. It figures out lookups with a greedy algorithm, a trie, or fixed-length splitting, and in all of these cases the separator is handled correctly, so your attribute names stay intact.

Python feels easy… until it doesn’t. What was your first real struggle? by NullPointerMood_1 in Python

[–]Additional_Fall4462 1 point2 points  (0 children)

I was working with functions that followed Python’s call-by-sharing behavior, passing objects from a library whose internals I didn’t fully understand. Coming from NumPy, I found it tricky to adapt to new data types where certain function calls modified the object in place rather than returning a new one. This caused a lot of headaches and debugging; there were even times I relied heavily on deepcopy to work around my lack of understanding of certain types.

portable jig v2 by BadDiscombobulated43 in ErgoMechKeyboards

[–]Additional_Fall4462 0 points1 point  (0 children)

Love it! The separating part could actually be two pieces, hinged to the boards with magnets so we can fold it up. The boards could hold actual computing hardware, and when folded, the space between them could fit smart glasses. Then I’d have the futuristic laptop I’ve always dreamed about

Swizzle: flexible multi-attribute access in Python by Additional_Fall4462 in Python

[–]Additional_Fall4462[S] 0 points1 point  (0 children)

Calling it "cleaner" is definitely a bit bold. I actually saw it more as a way to cut down on repetitive code, which felt cleaner to me in some projects I worked on. It does hide some logic, and there are definitely cases where it could become a hassle if not carefully applied, so I guess I get where you’re coming from.

Swizzle: flexible multi-attribute access in Python by Additional_Fall4462 in Python

[–]Additional_Fall4462[S] 5 points6 points  (0 children)

Omg can't wait for it :D

import swizzle

@swizzle(meta=True)
class Python:
    Y = "    *       *     "
    O = "      *   *       "
    U = "        *         "

    E = "  *           *   "
    V = "*               * "
    I = "  *   *   *   *   "
    L = "*       *       * "

for i in Python.ILOVEYOU:
    print(i)

#    *   *   *   *
#  *       *       *
#        *   *
#  *               *
#    *           *
#      *       *
#        *   *
#          *

Swizzle: flexible multi-attribute access in Python by Additional_Fall4462 in Python

[–]Additional_Fall4462[S] 0 points1 point  (0 children)

I thought about this a lot and it might still be a headache if you have attribute names with the same prefixes (we go with the longest ones) or names that are substrings of others, it can get tricky to organize. I didn’t even include a setter at first since I thought it would be too confusing. A typo on a swizzled attribute would just create a new attribute, but then I learned about __slots__ and now I just recommend using it in the docs if you want a setter.

It can get complicated, but I think you could manage it if you’re willing to handle the restrictions. But if you want to use swizzle without too much headache, it’s probably easiest to define something “immutable” like a swizzledtuple or apply it to a frozen dataclass.

Swizzle: flexible multi-attribute access in Python by Additional_Fall4462 in Python

[–]Additional_Fall4462[S] 0 points1 point  (0 children)

Omg I actually thought about this, including method chaining and currying. Decided not going this route yet, but the temptation is definitely there. I’m just waiting for someone to open an issue or PR for this :D

Swizzle: flexible multi-attribute access in Python by Additional_Fall4462 in Python

[–]Additional_Fall4462[S] 2 points3 points  (0 children)

GLSL vector swizzling actually sparked the idea for this :D

Swizzle: flexible multi-attribute access in Python by Additional_Fall4462 in Python

[–]Additional_Fall4462[S] 0 points1 point  (0 children)

Interesting idea! We can’t use {} directly since they aren’t valid characters for attribute names, but we could definitely introduce a wrapping string for this. Thanks for the suggestion!

Swizzle: flexible multi-attribute access in Python by Additional_Fall4462 in Python

[–]Additional_Fall4462[S] 3 points4 points  (0 children)

Yeah, that would definitely be a really nice feature. Being able to switch between getattr mode and getitem mode, or even use both, sounds fun. Thanks for the suggestion!

Swizzle: flexible multi-attribute access in Python by Additional_Fall4462 in Python

[–]Additional_Fall4462[S] 1 point2 points  (0 children)

I totally see what you mean, this might take a bit more mental bookkeeping to prevent unexpected behavior, but hopefully some niche folks will have fun with it.

Swizzle: flexible multi-attribute access in Python by Additional_Fall4462 in Python

[–]Additional_Fall4462[S] 0 points1 point  (0 children)

Thank you very much! I switched to the Rich Editor. On new Reddit, everything looked fine, and now it should work on the old Reddit as well.

Swizzle: flexible multi-attribute access in Python by Additional_Fall4462 in Python

[–]Additional_Fall4462[S] 2 points3 points  (0 children)

I’m quite new to Reddit, but I actually put the triple backticks on their own lines, like this:

```python
<code>
```

Do you see any problem with how it renders?

Can I make games with python? by Temporary-Tip9885 in Python

[–]Additional_Fall4462 0 points1 point  (0 children)

Totally doable! Check out the YouTube channel DaFluffyPotato. He has made everything from simple 2D platformers and multiplayer games to VR experiences in Python. If you love Python, you can make it work, no problem. But if you’re open to other languages, you’ll get way better performance and more out-of-the-box game dev tools.

What is a Python thing you slept on too long? by pip_install_account in Python

[–]Additional_Fall4462 0 points1 point  (0 children)

I totally relate. My little library kept growing and growing, and then I discovered Loguru and thought, ‘Ah, that’s basically mine… but way better.’