• Eager Eagle@lemmy.world
    link
    fedilink
    English
    arrow-up
    85
    ·
    2 months ago

    nah, I’m never complaining about self in Python after having tried the this and that nonsense in JS.

    oh, you’re using a named function instead of an arrow fn? Guess what, this is not what it used to be anymore.

    • panda_abyss@lemmy.ca
      link
      fedilink
      arrow-up
      28
      ·
      2 months ago

      Reminds me of java

      I have Toolkit toolkit = Toolkit.getDefaultToolkit(); seared into my brain. Then there were the bean factories…

      • Frezik
        link
        fedilink
        arrow-up
        16
        ·
        2 months ago

        At least with Rust, there is a specific, defensible goal for why it does that.

        Java is just over designed. All of java.io reads like somebody’s Object Orientated Programming 101 final project, and they’d get a B- for it. Lots of things where you can see how they’re abstracting things, but there’s no thought at all in bringing it together in a tidy way.

        • Fiery@lemmy.dbzer0.com
          link
          fedilink
          arrow-up
          5
          ·
          2 months ago

          Not like C# is all that much better. So much garbage in the fundamentals just because it was done that way at the start and “they can’t change it now”. The best example is the IList interface.

          Theoretically this interface exposes both index-based access and collection-like modifications and as such would be perfect in a function if you need those two features on a type. In reality you can’t use it as a function parameter because half the official types implementing IList aren’t modifiable and throw a runtime error. E.g Arrays

          • Kogasa@programming.dev
            link
            fedilink
            arrow-up
            2
            ·
            2 months ago

            That’s a footgun sure but at least you can avoid it once you’re aware of the problem.

            I never write function signatures with mutable interfaces. It’s always IEnumerable, IReadOnlyCollection, or IReadOnlyList; otherwise, use a concrete type. The latter is typical for private/protected methods that are called with instance members of a concrete type rather than public interfaces. If you want to mutate an object, you should own it. Public methods are invoked with data not owned by the instance.

            For example, a lot of extension methods in LINQ have a signature IEnumerable<T> --> IEnumerable<T>, and internally the first thing they do is call .ToList(). The interface makes minimal assumptions about the input data, then puts it into a concrete type you can manipulate efficiently. You can similarly define a method for IReadOnlyList and explicitly make it mutable via .ToList(), rather than use IList and check .IsReadOnly. Both ensure correctness but the former does it at the type level, at design time, instead of relying on runtime checks.

            C# is old and full of oldness. But it’s also an excellent language that can be written beautifully if you know how. And there’s lots of great code to learn from in the open-source dotnet core runtime repo and related projects.

            • Fiery@lemmy.dbzer0.com
              link
              fedilink
              arrow-up
              2
              ·
              2 months ago

              Functional programming fixes the problem by simply not making it OO anymore, and while I’m personally a big fan of the paradigm there are situations where an OO approach is preferable (or even only to conform to a project’s existing way of doing things).

              • Kogasa@programming.dev
                link
                fedilink
                arrow-up
                1
                ·
                2 months ago

                What I described isn’t necessarily functional. This is just a principle for ensuring objects represent clear and well-defined contracts. The idea is that to mutate something, you should own it; that means interfaces / public APIs, which can be called externally, should take immutable arguments. You can still mutate instance members internally because those are owned by the instance. If mutation is really necessary between two objects then it should be coordinated by an object owning them both.

    • shape_warrior_t@programming.dev
      link
      fedilink
      English
      arrow-up
      18
      ·
      2 months ago

      Even regular Rust code is more “exciting” than Python in this regard, since you have a choice between self, &self, and &mut self. And occasionally mut self, &'a self, and even self: Box<Self>. All of which offer different semantics depending on what exactly you’re trying to do.

      • dejected_warp_core@lemmy.world
        link
        fedilink
        arrow-up
        8
        ·
        2 months ago

        I’ll add that 100% of the above is understood by the compiler. Unlike Python or JavaScript where you don’t know how bad you have it until the program is already running.

        • Lightfire228@pawb.social
          link
          fedilink
          arrow-up
          7
          ·
          2 months ago

          At least python has a decent runtime typing system

          JS’s type system feels like what you’d get by giving a monkey access to unlimited cocaine and a computer

  • mesa@piefed.social
    link
    fedilink
    English
    arrow-up
    30
    ·
    2 months ago

    Explicit vs implicit. Ive always liked it being explicit like that. It’s better than magic keywords in say ruby.

    Personally the “spaces are code” gets on my nerves for the same reason. It’s implicit to the language so you just have to remember.

      • Frezik
        link
        fedilink
        arrow-up
        23
        ·
        2 months ago

        What 2003 forum thread did this comment crawl out of?

      • Kay Ohtie@pawb.social
        link
        fedilink
        English
        arrow-up
        10
        ·
        2 months ago

        C/C++ are just scripting languages that have to become OS+arch -specific byte-code before execution.

          • Kay Ohtie@pawb.social
            link
            fedilink
            English
            arrow-up
            12
            ·
            2 months ago

            Python is compiled at “runtime” to a similar OS+arch byte-code minus ELF headers that Linux binaries typically have from gcc.

            My point was it’s a stupid distinction and worthless when the other points about poor implementations of common language frameworks are plenty on their own is all, and it’s needlessly snobbish.

            As far as class variable reference however I wish more languages self-referenced. In my eyes it makes it far clearer at a given line of code glance as to where the hell a value came from as opposed to just by name. I feel a keyword like self::variableName, or maybe more aptly &self as a pointer to reference in C++ would be very clear, like Rust does, which is very much, by the original definition, a programming language instead of scripting. Even Java, which is definitely not a scripting language though is still run inside a virtual machine, uses this. I don’t personally like the term versus self, but eh.

            Though if you want a hammer in a screw-driven world look no further than Electron. I think it puts anyone else’s even purposeful attempts at such to shame.

      • Albbi@piefed.caOP
        link
        fedilink
        English
        arrow-up
        3
        ·
        2 months ago

        You’re right in that OOP feels very shoehorned in with Python. But not every project has a Linus Torvalds to publicly humiliate horrible ideas and implementations.

        • fruitcantfly@programming.dev
          link
          fedilink
          arrow-up
          7
          ·
          2 months ago

          In what way does OOP feel shoehorned in with Python? I ask since that is not my own impression of the language.

          Would you also be willing to share what language(s) you feel do(es) OOP without it being shoehorned in?

          • amos@lemmy.zip
            link
            fedilink
            English
            arrow-up
            4
            ·
            2 months ago

            Yeah, some weird accusations. Python has had classes since its inception (1.0).

            Also the image in the post makes no sense. It shows multiple (Spidey) instances all pointing to each other which is not how self works. self is just a parameter that may contain different instances depending how it was called. This is also true for any other parameters in any function, each time a function is called it may have a different instance.

          • Albbi@piefed.caOP
            link
            fedilink
            English
            arrow-up
            1
            ·
            2 months ago

            I was looking to see if there are equivalents to Java’s private and protected members, and it looks like Python’s answer to that is just throw one or two underscores in front of things to do that. And it doesn’t really do anything, more of just a naming convention. To me that feels like a basic OO structure that is shoehorned into Python.

            • fruitcantfly@programming.dev
              link
              fedilink
              arrow-up
              2
              ·
              2 months ago

              A single underscore is just a naming convention, but double underscores triggers automatic name-mangling of the variable in question:

              $ cat test.py
              class foo:
                      def __init__(self, x):
                              self.__x = x
              
              f = foo(1)
              f.__x
              $ python3 test.py
              Traceback (most recent call last):
                File "/mnt/d/test.py", line 6, in <module>
                  f.__x
              AttributeError: 'foo' object has no attribute '__x'
              

              However, much like private/protected variables in java, this is pretty trivial to circumvent if you want.

              But I don’t believe that you can argue that access modifiers are required for OO not to be shoehorned into a language, not when influential OO languages like Smalltalk didn’t have this feature either. Java just happens to be closer to C++, where public/private/protected is much more rigidly enforced than either Java or Python

      • dejected_warp_core@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        2 months ago

        that’s because anyone who develops oop in Python is mentally ill.

        Hard disagree there. I would argue that most “multi-paradigm” languages converge on the same features, given enough time to iterate. It’s not necessarily about hot-sauce. I honestly think its about utility and meeting your userbase where their heads are.

    • Albbi@piefed.caOP
      link
      fedilink
      English
      arrow-up
      5
      ·
      2 months ago

      Write a new method, make sure to reference self first. Write a new method, make sure to reference self first. Call the method, make sure to reference self first.

      Yeah, I can see it.

      • amos@lemmy.zip
        link
        fedilink
        English
        arrow-up
        5
        ·
        2 months ago

        You don’t reference self when calling a method, what on earth are you talking about? You start with the instance when calling the method, like most/all other OOP languages.

        Also there are benefits with the explicit self/this to access instance properties. In C++ you need to make sure all class properties/members have a naming scheme that does not conflict with potential parameter names or other names of other variables.