Question:
Python classes help?
anonymous
2020-12-02 15:21:44 UTC
I'm sorry if this is a stupid question but I don't understand. Why does this example need the set methods like "set_name?" 

What is the difference between that and the __init__ method?
Four answers:
anonymous
2020-12-04 03:17:50 UTC
use set_name method to update the customer name.

c = Customer('name', 'address', 'phone')

c.set_name('new name')
Daniel H
2020-12-02 20:17:25 UTC
From the concept that other other objects/functions should not have direct access to member data.



You can set the data at creation or call member functions later
?
2020-12-02 17:02:40 UTC
You don't necessarily need setter methods for your python classes. In python everything is public. This is just a concept copied from other languages that do object oriented programming.

So you could get away with setting the name, address or phone directly. It's just that it would be considered "unsafe" to do so.

To give you an example: If I had a class named "DateTime" and I wanted to set the time for an object of this type as "Tue 02-Dec-2020 13:45 UTC +0020", then I would really like to use a method "setTime" to do so. The alternative would be to take a look at the whole class, understand how it works, figure out myself how to break down the time string and individually set every single member variable of my object to the correct value. This is tedious, unsafe, looks bad and error prone. Which is why we opt to use classes and setter/getter methods to hide implementation details like the one I just mentioned. The bad thing is schools tend to require students to adopt this mentality without having first seen the worse alternative. Sort of like "Follow the beaten path! Not because it's better, but because I said so!"



The second thing you asked is what is the difference between __init__ and the setter/getter methods.

Here, you should remember that there are usually 2 types of methods in object oriented programming languages (this is because humans prefer syntactic sugar).

The first kind are the ones you have to explicitely call. In your case "getPhone", "setAddress" etc. In order to execute the code of these functions, you will have to write out the whole name: myobject.doSomething()



The second kind are sort of "trigger functions". You can't call them by name, because they have no name. Instead you have to do specific operations in order to trigger their execution. In your case, __init__ will be called at object initialization time, i.e. when you do

Some of these functions are allowed to take parameters, but, for example, __str__ is not. __str__ can be triggered by attempting to convert the object to a string, i.e. doing something like: . As you can see, there is no way to pass any arguments to the implicitly called __str__ method.

These keywords are baked into the python interpreter and there is a set number of them. Coming up with new names like __dosomething__ would not make any sense for the interpreter and it will just register your method as a regular one.
EddieJ
2020-12-02 15:27:18 UTC
The __init__ method gets called when the object is instantiated (created).



The set methods are used when you want to change the value of an object that already exists.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...