[Q] __init__ in class
DL Neil
PythonList at DancesWithMice.info
Thu Dec 26 19:56:04 EST 2019
On 27/12/19 10:58 AM, Irv Kalb wrote:
>> On Dec 26, 2019, at 3:11 AM, 황병희 <soyeomul at doraji.xyz> wrote:
>> in making class, why we should set "__init__"? so non-init class is
>> useless? actually i did fail to understand __init__'s meaning. what is
>> role of __init__ in class.
...
> As an example, let's say that you create a point class, where the Point has both an x and a yY value. Your class might look like this:
>
> class Point:
> def __init__(self, x, y): # When you create a Point object, you must pass in a value for x and y
> self.x = x
> self.y = y
>
> #Then you would probably have other methods like:
>
> def getX(self):
> return self.x
>
> def getY(self):
> return self.y
>
> def show(self):
> print('The values of this point are:', self.x, self.y)
>
> etc.
>
> Given a class like that, when you create an object from the Point class, you pass in values for x and y. The code in the __init__() method saves away those values into instance variables. Those instance variables can be used by other methods. In this example class, getX, getY, and show.
>
> Here is some sample code to create two Point objects, then ask them to show their contents:
>
> pointA = Point(10, 15)
> pointB = Point(-3, 38)
> pointA.show()
> printB.show()
Is this a/the pythonic way to access attributes?
It appears Java-like, or some other language transliterated into Python.
Further, the naming convention suffers similar heritage.
What was not shown is that in order to access the values of Point()'s x
and y attribute, the following is needed:
x_value = pointA.getX()
which in and of itself is not too bad, if wordy and ugly; but what
happens when we perform a vector addition?
point_a_plus_b = Point( pointA.getX() + pointB.getX(),
pointA.getY() + pointB.getY()
)
Which is still not too shocking, but imagine a more complicated
calculation, eg widen to include z- and w- dimensions or consider
n-dimensional matrices...
(there are optimised tools for such, but continuing with the post-er's
example)
Rather than a 'closed' view of objects ("encapsulation") Python
maintains a more 'open' approach. "Getter" and "Setter" functions are
put-aside in favor of direct-access:
value = instance.attribute # 'getter'
instance.attribute = value # 'setter'
del instance.attribute # 'destructor'
Now, imagine a class with ten attributes (for example) in Java (and
similar). Before use, we would have to write ten getter-functions, ten
setter-functions, and maybe (just to be 'complete') ten
destructor-functions. Whereas in Python, nothing!
Whatever you do, don't mention this to such coder-colleagues! We are
able to code so much faster than they, and are considerably less
restricted by our choice of language, thus promoting numerous efficacies!
There are times when we can't allow 'just any value' to be applied to an
attribute. For example, should a person's birth-date be recorded as
ten-years from now? So, when we need to check or 'massage' data before
use/recording, we might turn the attribute into a "property". This does
give us the Python (and pythonic) version of getters-and-setters.
Similarly, there are tactics which do enable data-hiding (or strict
"encapsulation") for those occasions when it is needed...but now we're
likely way beyond the OP's needs!
WebRefs: (additional to those previously-posted in this thread)
https://www.python-course.eu/python3_properties.php
https://www.python.org/dev/peps/pep-0008/
--
Regards =dn
More information about the Python-list
mailing list