Coder Social home page Coder Social logo

Formatting around "and" about blue HOT 4 OPEN

grantjenks avatar grantjenks commented on July 17, 2024 1
Formatting around "and"

from blue.

Comments (4)

grantjenks avatar grantjenks commented on July 17, 2024 5

I think the ideal here is:

     def __eq__(self, that):
         if not isinstance(that, type(self)):
             return NotImplemented
         return (
            self.__slots__ == that.__slots__
            and all(item == iota for item, iota in zip(self, that))
        )

from blue.

wbolster avatar wbolster commented on July 17, 2024 3

oh hai,

imo, the ‘ideal’ is far from ideal.

  • for starters, this code implicitly depends on the type being iterable.
  • on top of that, it implicitly assumes that iteration yields the values worth comparing, which may or may not be the same as having equal values in each field defined in __slots__
  • the variable ‘that’ would contrast with a variable ‘this’, but this is python, which uses ‘self’. so ‘self’ / ‘other’ would be a better choice.
  • a variable named ‘iota’ means nothing
  • a variable ‘item’ compared to ‘iota’ doesn't mean much either

that said, here's a quick attempt that would be closer to ‘ideal’, and lo and behold, it doesn't suffer from perceived formatting ‘problems’ either:

class C:
    def __eq__(self, other):
        if not isinstance(other, type(self)):
            return NotImplemented

        if self.__slots__ != other.__slots__:
            return False

        for x in self.__slots__:
            if getattr(self, x) != getattr(other, x):
                return False

        return True

the ‘ideal’ solution would be to not write any code at all, and use attrs instead to declaratively define attributes and whether they should be included in an an equality comparison.

from blue.

merwok avatar merwok commented on July 17, 2024

This ticket is not a discussion of that __eq__ method, but of the formatting of specific lines by the black tool.

from blue.

wbolster avatar wbolster commented on July 17, 2024

which is way more subtle and complex than this ticket seems to imply; see psf/black#2156 (comment)

from blue.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.