• WolfLink@sh.itjust.works
    link
    fedilink
    arrow-up
    34
    ·
    25 days ago

    In Python, self is not a keyword, it’s a conventional variable name. You can replace all instances of “self” with “this” and your code will work the same.

      • Die Martin Die@sh.itjust.works
        link
        fedilink
        arrow-up
        5
        ·
        edit-2
        23 days ago

        Kinda.

        Lua defines it implicitly only when you use the

        function foo:bar(a, b, c) -- note the colon
        

        syntactic sugar, which gets translated to

        function foo.bar(self, a, b, c) -- note the period
        

        In all cases, self is a regular variable name. You can even redeclare a new local with that name even when the old one is in scope.

        Edit: some typos

    • Pennomi@lemmy.world
      link
      fedilink
      English
      arrow-up
      6
      arrow-down
      1
      ·
      edit-2
      25 days ago

      In Python you can use 🍆 as a variable name.

      Edit: oops, guess I was mistaken, you can use most Unicode but emojis are not valid.

    • ryedaft@sh.itjust.works
      link
      fedilink
      English
      arrow-up
      3
      ·
      25 days ago

      You can use anything that doesn’t start with a digit or punctuation as a variable name (underscore beginning also allowed) unless it’s a keyword.

      • funkless_eck@sh.itjust.works
        link
        fedilink
        arrow-up
        3
        ·
        edit-2
        25 days ago

        _ (sic) as a variable name is often used when a function returns multiple outputs but you only want one

         def my_function:
              return 1, 2, 3
        
         _, two, _ = my_function()
        
        • ryedaft@sh.itjust.works
          link
          fedilink
          English
          arrow-up
          2
          ·
          edit-2
          24 days ago

          Underscore alone is a special variable name and I’m pretty sure anything assigned to it goes straight to garbage collection. Whereas _myvariable is typically used to indicate a “private” class variable or method (Python doesn’t have private so it’s just a convention).

        • Archr@lemmy.world
          link
          fedilink
          arrow-up
          1
          ·
          22 days ago

          _ can also be used in the python interactive terminal to mean ‘last return value’

          Ie:

          > 'string'
          'string'
          > a = _
          > print(a)
          string
          
  • xiii@lemmy.world
    link
    fedilink
    arrow-up
    6
    ·
    edit-2
    24 days ago

    I was working on a C code base with classes, inheritance, and polymorphism, all done by hands and macros.

    Something like

    typedef struct s_some_class {
        void (*method)(this *s_some_class);
    } t_some_class;
    

    Overall, learning C was the best enabler in my whole career. For instance I was learning Python by tinkering with CPython VM, so when I see these ‘WAT’ quircks I know exactly what’s up.

    • bestelbus22@lemmy.worldOP
      link
      fedilink
      arrow-up
      3
      ·
      24 days ago

      Interesting, how did they do inheritance? Something like void *super? Also why not switch to CPP if you wanna do OOP?

      • xiii@lemmy.world
        link
        fedilink
        arrow-up
        2
        ·
        23 days ago

        In general, ‘classes’ declarations were done with macro. I don’t remember the exact code — something akin to

        BEGIN_CLASS(A, Parent);
        CLASS_MEMBER(a...)
        END_CLASS();
        

        The project had started before C++ existed, and the switch would be too costly. It’s not just OOP part, also reflection mechanism with bindings to the homemade scripting language, and multi-platform UI library. It was a gem of its time.

          • xiii@lemmy.world
            link
            fedilink
            arrow-up
            2
            ·
            23 days ago

            Revolutionary technologies of the '80 make me appreciate modern programming languages and especially tooling much more.

  • Demdaru@lemmy.world
    link
    fedilink
    arrow-up
    4
    ·
    25 days ago

    Not much experience, but quickly learned .bind() in JS after it switched me to window instead of object.