• We can call methods of parenet class without initliaze it?

    From scruel tao@21:1/5 to All on Wed Mar 15 09:57:39 2023
    The following code won’t be allowed in Java, but in python, it works fine: ```python
    class A:
    A = 3

    def __init__(self):
    print(self.A)

    def p(self):
    print(self.A)
    self.A += 1


    class B(A):
    def __init__(self):
    print(2)
    self.p()
    super().__init__()


    B()
    ```

    How can I understand this? Will it be a problem?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to scruel tao on Wed Mar 15 10:46:17 2023
    scruel tao <scruelt@hotmail.com> writes:
    The following code won't be allowed in Java, but in python,
    it works fine

    In Usenet, it is recommended to write only a short
    noun phrase into the subject, and everything
    the reader needs to know, into the body of the message.

    class A:
    A = 3

    def __init__(self):
    print(self.A)

    AFAIK, Usually, one does not write "A = 3" into the body
    of a class. One ususally writes "self.A = 3" into the body
    of the "__init__" method instead. If you do this, then you
    might get an error message if you try to use the value of
    the field without having called "__init__" before.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Greg Ewing@21:1/5 to scruel tao on Wed Mar 15 23:54:42 2023
    On 15/03/23 10:57 pm, scruel tao wrote:
    How can I understand this? Will it be a problem?

    I can't remember any details offhand, but I know I've occasionally
    made use of the ability to do this. It's fine as long as the method
    you're calling doesn't rely on anything you haven't initialised yet.

    --
    Greg

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Roel Schroeven@21:1/5 to All on Wed Mar 15 13:01:49 2023
    Op 15/03/2023 om 10:57 schreef scruel tao:
    The following code won’t be allowed in Java, but in python, it works fine: ```python
    class A:
    A = 3

    def __init__(self):
    print(self.A)

    def p(self):
    print(self.A)
    self.A += 1


    class B(A):
    def __init__(self):
    print(2)
    self.p()
    super().__init__()


    B()
    ```

    How can I understand this? Will it be a problem?
    Important: __init__ is not a constructor, like you have for example in
    C++. I don't know Java, but it seems plausible it works somewhat like
    C++ in this regard. Python does it differently: when you create an
    instance, the instance is fully created/constructed even before __init__
    is called. __init__ is purely an initializer: you can use to initialize
    the things you want to initialize.

    Back to your example: it works because A is a class-level attribute,
    which is initialized independently from __init__. If you make it an
    instance attribute, like below, things stop working:

        class A:
            def __init__(self):
                self.A = 3
                print(self.A)

            def p(self):
                print(self.A)
                self.A += 1


        class B(A):
            def __init__(self):
                print(2)
                self.p()
                super().__init__()


        B()
        print(A.A)

    That fails like this:

        Traceback (most recent call last):
          File ".code.tio", line 18, in <module>
            B()
          File ".code.tio", line 14, in __init__
            self.p()
          File ".code.tio", line 7, in p
            print(self.A)
        AttributeError: 'B' object has no attribute 'A'

    That's because now A is indeed initialized in A.__init__, so it doesn't
    exist before A.__init__ is called.

    --
    "Too often we hold fast to the cliches of our forebears. We subject all
    facts to a prefabricated set of interpretations. Too often we enjoy the
    comfort of opinion without the discomfort of thought."
    -- John F Kennedy

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)