Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python classes
#11
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:
return 'Hello world {0}'.format(self.punctuation)
or
return 'Hello world ' + self.punctuation
Do 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.
Reply
#12
Quote: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.
Am I correct when I understand the above quote as: An class should normally exists of one method which is the __init__ method, but more methods are used to make the code more 'clear'?

Quote: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.
Thank you for this description, very clear and understandable :)
Reply
#13
Usually a Python class will have several methods. __init__ is usually one of those methods, but it doesn't have to be. It is not required that a Python class has an __init__ method.
Reply
#14
Aha, but what's makes the __init__ method different from the other methods? The python docs says:
Quote:Many classes like to create objects with instances customized to a specific initial state. Therefore a class may define a special method named __init__()

I can't place why an __init__ method is used...
Reply
#15
The __init__() method is run automatically by python when the instance is created/initialized. You don't have to invoke it. So it's a great place to put setup information for the instance.

Other methods have to be explicitly called (by your code).
Reply
#16
(Aug-03-2020, 08:13 PM)bowlofred Wrote: The __init__() method is run automatically by python when the instance is created/initialized. You don't have to invoke it. So it's a great place to put setup information for the instance.

Other methods have to be explicitly called (by your code).

Thank you bowlofred, I think I do understand it know :).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Classes rob101 4 527 Feb-05-2024, 06:51 PM
Last Post: rob101
  Understanding Python classes PythonNewbee 3 1,183 Nov-10-2022, 11:07 PM
Last Post: deanhystad
Sad Python classes PythonNewbee 4 1,046 Nov-09-2022, 01:19 PM
Last Post: deanhystad
  Inheritance vs Instantiation for Python classes mr_byte31 7 2,849 Oct-14-2021, 12:58 PM
Last Post: mr_byte31
  Understanding Python super() for classes OmegaRed94 1 1,826 Jun-09-2021, 09:02 AM
Last Post: buran
  Python Classes leodavinci1990 1 2,079 Nov-27-2019, 07:25 AM
Last Post: buran
  Using classes? Can I just use classes to structure code? muteboy 5 5,033 Nov-01-2017, 04:20 PM
Last Post: metulburr
  use of classes in python Lux 2 3,510 Aug-19-2017, 12:29 PM
Last Post: hbknjr
  python classes prsdll6 14 7,368 Aug-17-2017, 07:26 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020