Aug-02-2020, 03:49 PM
(This post was last modified: Aug-02-2020, 03:49 PM by deanhystad.)
punctuation could be an argument to "__init__" but it doesn't have to be. Many classes initialize instance variables to a default value in the __init__ method. Usually this is done because the default value is a commonly used value and there are methods for changing the value. Quite often it is done this way because the class has too many instance variables for them to all be specified in the __init__ args.
f' is the new way to do str.format(). In this particular instance it is the same as:
I have seen a few posts in this forum with code that defines the same function multiple times with the only difference being the function arguments. This is obviously from somebody who usually programs in a language where the function arguments are part of the function signature. This is not how Python works. Python has a name that is used to get a value. When Python executes
Classes are no different than 'x'. You can set a class variable or a class method multiple times and it only remembers the last. Instance variables work this way too.
So now to answer you last question. In this poorly written class there are multiple __init__ methods. Which of these does Python know about? Python reads files from top to bottom, so the bottom __init__ method is the only __init__ method. That is the only __init__ method the class remembers.
Inside the second __init__ punctuation is set twice. Which value is saved in punctuation? The bottom one.
The class also sets classvar twice. Which value is saved in classvar? The bottom one.
f' is the new way to do str.format(). In this particular instance it is the same as:
return 'Hello world {0}'.format(self.punctuation) or return 'Hello world ' + self.punctuationDo not worry about the details of the example. The example is to show that you can define the same name to be multiple things, and that the only mapping between name and thing is the last one.
I have seen a few posts in this forum with code that defines the same function multiple times with the only difference being the function arguments. This is obviously from somebody who usually programs in a language where the function arguments are part of the function signature. This is not how Python works. Python has a name that is used to get a value. When Python executes
x = 5
it first looks for 'x' in the local scope. If not found it adds 'x' to the local scope. Next it maps 'x' to 5 by setting 'x' to 5. If later on Python executes x = 7
, the same thing is repeated, but now 'x' is set to 7. There is no way to get back to 5 through 'x'. 'x' does not remember anything about previous values. It only remembers the current value.Classes are no different than 'x'. You can set a class variable or a class method multiple times and it only remembers the last. Instance variables work this way too.
So now to answer you last question. In this poorly written class there are multiple __init__ methods. Which of these does Python know about? Python reads files from top to bottom, so the bottom __init__ method is the only __init__ method. That is the only __init__ method the class remembers.
Inside the second __init__ punctuation is set twice. Which value is saved in punctuation? The bottom one.
The class also sets classvar twice. Which value is saved in classvar? The bottom one.