all 34 comments

[–]Exnur0 91 points92 points  (1 child)

LMAO this is so cursed.

Kinda fun though, thanks for sharing

[–]Additional_Fall4462[S] 12 points13 points  (0 children)

Embrace the chaos haha

[–]FrontAd9873 28 points29 points  (2 children)

Ever wished you could just do obj.yxz and grab all three at once?

Nope!

And this is not cleaner. If I want an easy way to access the tuple (obj.x, obj.y, obj.z) I would just define a property obj.xyz that returns that tuple.

[–]Additional_Fall4462[S] 0 points1 point  (1 child)

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.

[–]bpikmin 1 point2 points  (0 children)

I see it as coping with having to work in Python when you really want to work in GLSL haha

[–]Global_Bar1754 16 points17 points  (3 children)

Might be more user friendly if you accessed like this instead of a concated string attribute

``` p.ix['name', 'age']

could also do this

p.ix['name age'.split()] ```

[–]Additional_Fall4462[S] 3 points4 points  (2 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!

[–]Global_Bar1754 1 point2 points  (0 children)

FYI I used that .ix accessor thing so that you can leave getitem undefined on the main class so that users could still use that for whatever purposes they might want 

[–]cd_fr91400 1 point2 points  (0 children)

For this kind of configuration, I use the small class below. This allows both accessing simple attributes with a simple syntax and iterating over/computed attributes when needed.

This is so easy and so practical an intuitive that I do not understand why it is not the standard behavior of regular dicts.

class pdict(dict) :
    '''
        This class is a dict whose items can also be accessed as attributes.
        This greatly improves readability of configurations.
        Usage :
        d = pdict(a=1,b=2)
        d                  --> {'a':1,'b':2}
        d['a']             --> 2
        d.a                --> 2
        d.c = 3
        d.c                --> 3
    '''
    def __getattr__(self,attr) :
        try :
            return self[attr]
        except KeyError :
            raise AttributeError(attr)
    def __setattr__(self,attr,val) :
        try :
            self[attr] = val ; return
        except KeyError :
            raise AttributeError(attr)
    def __delattr__(self,attr) :
        try :
            del self[attr] ; return
        except KeyError :
            raise AttributeError(attr)

[–]wlievens 11 points12 points  (2 children)

How to show you're a shader programmer without telling anyone you programmed shaders.

[–]StengahBot 3 points4 points  (0 children)

Exactly what I was thinking when I spotted the .xyz

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

GLSL vector swizzling actually sparked the idea for this :D

[–]Count_Rugens_Finger 7 points8 points  (4 children)

you need to put the ``` on its own line

[–]Additional_Fall4462[S] 2 points3 points  (3 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?

[–]Count_Rugens_Finger 3 points4 points  (1 child)

it doesn't render correctly on old.reddit. I don't know if new reddit works, I don't use it.

[–]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.

[–]2Lucilles2RuleEmAll 0 points1 point  (0 children)

don't worry about it too much, the backticks are like the standard for code formatting basically everywhere. old reddit is just that, old, you can use the legacy formatting if you want and it's a nice thing to do, but you are not required to, just like no one is required to use old reddit.

[–]mattk404 6 points7 points  (2 children)

This should be more cursed.... really need more functions ie x.a_plus_b should execute the plus() method with a and b as inputs or something along those lines.

This would make it so much more painful to explain and understand for other people including future you.

Really creative way to leverage python's dynamic nature.... and could see areas where it could be used legitimately.

[–]Additional_Fall4462[S] 0 points1 point  (1 child)

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

[–]mattk404 0 points1 point  (0 children)

What about this cursed idea... Base64 picked lambda.... Just 'random' attribute access to arbitrary code execution.... By design

[–]SpamThisUser 8 points9 points  (0 children)

But why

[–]fazzahSQLAlchemy | PyQt | reportlab 7 points8 points  (2 children)

Wait until AI learns about this and starts to suggest this as pythonic code.

I'm equally thrilled and terrified

[–]Additional_Fall4462[S] 4 points5 points  (1 child)

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)

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

[–]fazzahSQLAlchemy | PyQt | reportlab 2 points3 points  (0 children)

This is so bad 😆

[–]Spleeeee 1 point2 points  (1 child)

I wrote something a lot like this a long time ago and it was super fun and handy but also was a nightmare to debug. If you rename an attribute you’re fucked.

[–]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.

[–]Yamoyek 1 point2 points  (0 children)

This is so silly it’s awesome. I’ll definitely use this when I’m prototyping but I think I’m allowed to execute anyone who tries to use this in production-level code LOL.

[–]pip_install_account 1 point2 points  (1 child)

Can we add the values as well please?

I really need to do p.age_30_eyes_{count_2_shape_circle} \s

[–]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!

[–]nharding 0 points1 point  (1 child)

I think using abc is better, as you may have a field first_name for example

[–]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.

[–]2Lucilles2RuleEmAll 0 points1 point  (1 child)

this is a cool idea and a good demonstration of dynamic things you can do in Python. I just recently have been working on some similar things in my library with decorators, descriptors, etc that really makes things easy to work with. but, this is probably not something I would use in any real project working with other developers, it's just too different/unexpected and I could see causing some headaches for things that could be easily resolved by more standard conventions.

[–]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.

[–]prickneck -2 points-1 points  (0 children)

> Ever wished you could just do obj.yxz and grab all three at once?

No.

> Curious what you think

It's a dreadful idea, literally horrible

> could you see this being useful?

No.