On Oct 21, 2019, at 07:44, gedizgu...@gmail.com wrote:
> 
> m and n are lists or dicts or enumerates or classes or anything it can be 
> assigned like following:

Assigning attributes is different from assigning items to a collection like a 
list or dict. That’s why there are separate syntaxes, and separate protocols 
for it.

I realize that in JavaScript that’s not true; collections are just objects with 
their items as attributes, so `a.b` means the same thing as `a['b']`. But 
that’s confusing more often than it’s helpful, and I don’t think anyone wants 
to bring it to Python, or any other languages.

> instead of :
> 
> m.a=n.a;
> m.b=n.b;
> m.c=n.c;
> ...
> 
> I suggest:
> 
> a,b,c of m to n ;

You can do this with a two-liner function:

    copy_attrs(m, n, 'a', 'b', 'c')

… which you implement like this;

    def copy_items(src, dst, keys):
        for key in keys:
            dst[key] = src[key]

    def copy_attrs(src, dst, *attrs):
        for attr in attrs:
            setattr(dst, attr, getattr(src, attr))

Or, if you want to check that all of the source attributes exist and raise 
before writing anything to dst:

        values = [src[key] for key in keys]
        for (key, value) in zip(keys, values):
            dst[key] = value

Or maybe instead of *attrs you want to take a single string and split() it, 
similar to the way namedtuple does.

The functions are simple to write, and probably wouldn’t be used often, and 
there are variations that you’d probably want sometimes one way and sometimes 
the other. So I don’t think this needs to be in the stdlib, much less needs 
custom syntax. But I could be wrong. If you disagree, you can:

 * Try to argue for good use cases that would come up frequently. 
 * Search the stdlib, popular projects on PyPI, in your own codebase, etc. to 
come up with examples that would be more readable with this new feature.
 * Search places like python-list and StackOverflow to see if people are asking 
for this feature (or, even better, if people are confused but would be asking 
for this feature if they knew what they wanted).
 * Package it up and publish on PyPI and come back later with stats showing 
lots of people downloading and using it.

As a side note: Python statements don’t end in semicolons like JavaScript. It’s 
usually legal to put one there (it means you’re adding an extra null statement 
after each statement, which is wasteful but otherwise has no effect that will 
usually break anything). But you shouldn’t do it.

More generally, don’t try to write Python as if it were JavaScript. They have 
different idioms for a lot of things, and trying to treat them the same means 
you end up writing Python code that isn’t very good Python and JS code that 
isn’t very good JS. 

> 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;

Another way people have tried to solve this problem is with a BASIC-style 
`with` (which has nothing to do with Python’s `with`):

    with adiv.style:
        .y = comp.y
        .height = comp.height

I don’t think that’s really helpful here, because you have two different 
objects you want to shortcut, not just one. But you might want to look into 
past proposals to add a similar feature to Python, to see if any of them did 
come up with something that would be helpful to you.
_______________________________________________
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/NUGWHXCVN5LZZLDX74R7CVDV2QPRTTQQ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to