Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting PEP 448 Python 3.5 code to Python 3.4-compatible

I have the following function:

    def to_url(self):
    return {
        'ass_cls': self.model.__class__.__name__,
        **{local.name: getattr(self.model.src, remote.name)
           for local, remote in self.model.__class__.src.property.local_remote_pairs},
        **{k: v
           for k, v in self.model.__dict__.items()
           if not k.startswith('_') and k != 'src'},
    }

How do I convert this piece of code into Python 3.4-compatible?

I believe, the code currently uses PEP 448 - Additional Unpacking Generalizations right now, which is a Python 3.5 feature.

like image 932
kirillbobyrev Avatar asked Jul 15 '26 14:07

kirillbobyrev


1 Answers

The new unpacking feature is what won't work in 3.4.

You'll have to use the older, more verbose method of merging dictionaries.

def to_url(self):
    d = {'ass_cls': self.model.__class__.__name__}
    d.update({local.name: getattr(self.model.src, remote.name) 
        for local, remote in self.model.__class__.src.property.local_remote_pairs})
    d.update({k: v for k, v in self.model.__dict__.items()
        if not k.startswith('_') and k != 'src'})
    return d
like image 53
Brendan Abel Avatar answered Jul 18 '26 14:07

Brendan Abel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!