Steve Jorgensen wrote:
> gedizgursu@gmail.com wrote:
> > m and n are lists or dicts or enumerates or classes
> > or anything it can be assigned like
> > following:
> > instead of :
> > m.a=n.a;
> > m.b=n.b;
> > m.c=n.c;
> > ...
> > I suggest:
> > a,b,c of m to n ;
> > I thought of this while writing something in javascript since I love pyhton 
> > above
> > everything I can type (even more than native language sentences) I wanted 
> > to post this
> > idea to here
> > It is my first idea while coding ...
> > In javascript, I was assinging a boundingBoxRect values to a div it was 
> > going like
> > this:
> > adiv=document.createElement("div")
> > adiv.style.x=comp.x;
> > adiv.style.y=comp.y;
> > adiv.style.height=comp.height;
> > adiv.style.widht=comp.width;
> > I thought it is super waste of time instead of :
> > x,y,height,width of comp to adiv;
> > How about something like…
>     setattrs(adiv.style, getattrs(comp, 'x y height width')
> where getattrs returns a dict and setattrs accepts a
> mapping?
> That could be done now without adding any new features to the language. These 
> functions
> could be added to the standard library somewhere if they should become 
> official
> things.

Those might not be the best names for those functions, but continuing with 
those for now, here's an example implementation:

    from functools import reduce
    import re
    
    
    def getattrs(obj, *names):
        names = reduce(_concat_attr_names, names, [])
        return {name: getattr(obj, name) for name in names}
    
    
    def setattrs(obj, mapping):
        for name, value in mapping.items():
            setattr(obj, name, value)


    def _concat_attr_names(x, y):
        return x + re.split(r'\s+', y)
    
    
    class Thing:
        def __init__(self, color=None, size=None, shape=None):
            self.color = color
            self.size = size
            self.shape = shape
    
        def __repr__(self):
            return (
                f'Thing({repr(self.color)}, {repr(self.size)},'
                f' {repr(self.shape)})')
    
    
    thing_a = Thing('red', 'large', 'round')
    thing_b = Thing('blue')
    
    setattrs(thing_b, getattrs(thing_a, 'size shape'))
    
    print(repr(thing_b))  # -> 'Thing('blue', 'large', 'round')
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/Z4SY7U6RQCVPEGHU3VOABDC6LYKTVZNL/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to