[Q] __init__ in class

2019-12-26 Thread 황병희
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.

sincerely, from python beginner.

-- 
^고맙습니다 _地平天成_ 감사합니다_^))//
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Q] __init__ in class

2019-12-26 Thread DL Neil via Python-list

On 27/12/19 12:11 AM, 황병희 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.

sincerely, from python beginner.



Please be aware that there is a Python Tutor Discussion List.

If you are a beginner in Object-Oriented Programming, Wikipedia offers a 
multi-language, if dense introduction. This is complemented by their 
Class (computer programming) page.


If you are new to Python's implementation then don't go past the Python 
Tutorial (in particular Section 9).


There are plenty of web-tutorials discussion Python classes. 
HackerEarth's aligns nicely with the Wikipedia definitions (or the 
abstract terminology introduced in many ComSc texts and courses).



WebRefs:
Python Tutor: https://mail.python.org/mailman/listinfo/tutor
Object-oriented programming: 
https://en.wikipedia.org/wiki/Object-oriented_programming
Class (computer programming): 
https://en.wikipedia.org/wiki/Class_(computer_programming)

The Python Tutorial: https://docs.python.org/3/tutorial/index.html
HackerEarth: 
https://www.hackerearth.com/practice/python/object-oriented-programming/classes-and-objects-i/tutorial/

--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: [Q] __init__ in class

2019-12-26 Thread Irv Kalb
> On Dec 26, 2019, at 3:11 AM, 황병희  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.
> 
> sincerely, from python beginner.
> 
> -- 
> ^고맙습니다 _地平天成_ 감사합니다_^))//
> -- 
> https://mail.python.org/mailman/listinfo/python-list

Whenever you create object from a class, you use the name of the class in a 
statement like this:

myObject = MyClass()   #  "MyClass" would be the name of a class in your code

When you run a line like the one above, Python looks to see if that class has a 
method named __init__().  If it does, then Python calls that method of the 
newly created object.  In that method you typically put the initialization of 
any instance variables that you want to use in other methods, and include any 
additional code that you only want to run when an object is created from the 
class.  While it is not required to have an __init__() method in a  class, it 
is generally considered good practice to have one.  

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()

When run, this will print:

The values of this point are: 10, 15
The values of this point are: -3, 38

Hope that gives you a start in the world of OOP.

Irv

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Q] __init__ in class

2019-12-26 Thread DL Neil via Python-list

On 27/12/19 10:58 AM, Irv Kalb wrote:

On Dec 26, 2019, at 3:11 AM, 황병희  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
--
https://mail.python.org/mailman/listinfo/python-list


Re: [Q] __init__ in class

2019-12-26 Thread 황병희
Hellow Stefan^^^

>   The class definition depends on the requirement
>   specification for the class.
>
>   클래스 정의는 클래스의 요구 사항 스펙에 따라 다릅니다.

Now i got it, understood, thanks a lot^^^

Sincerely,

-- 
^고맙습니다 _地平天成_ 감사합니다_^))//
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Q] __init__ in class

2019-12-26 Thread 황병희
Hellow DL^^^

> Please be aware that there is a Python Tutor Discussion List.

Thanks for advice^^^

Sincerely,

-- 
^고맙습니다 _地平天成_ 감사합니다_^))//
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Q] __init__ in class

2019-12-26 Thread 황병희
Irv Kalb  writes:

> myObject = MyClass()   #  "MyClass" would be the name of a class in your code
>
> When you run a line like the one above, Python looks to see if that
> class has a method named __init__().  If it does, then Python calls
> that method of the newly created object.

Very clean comments, thank you Irv^^^ INDEED.

Sincerely,

-- 
^고맙습니다 _地平天成_ 감사합니다_^))//
-- 
https://mail.python.org/mailman/listinfo/python-list