0

I am new to Python and I have just created a class as part of the online course I am taking.

from math import sqrt

class Line:

def __init__(self,coor1,coor2):
    self.coor1=coor1  #tuple (x1,y1)
    self.coor2=coor2  #tuple (x2,y2)

def distance(self):
    return sqrt((self.coor2[0]-self.coor1[0])**2+(self.coor2[1]-self.coor1[1])**2)

def slope(self):
    return (self.coor2[1]-self.coor1[1])/(self.coor2[0]-self.coor1[0])

This is a class for Line and helps me find the distance between two coordinates. I am wondering, since a coordinate needs to be a tuple, how does Python know this? Why don't I need to define this in the def __init__?

gstukelj
  • 2,291
  • 1
  • 7
  • 20
Sorath
  • 543
  • 3
  • 10
  • 32
  • 1
    Another angle: Why would python need to know that your arguments have to be tuples? – Aran-Fey Oct 12 '19 at 12:42
  • Python doesn't care if you use tuple or something different - ie. strings or integers. You can use `if isinstance(coor1, tuple):` to use only tuples but you still can put tuple with 3 or more numbers or with strings - `Line( ("Hello", "World"), ("Anonther", "tuple", "with", "strings") )` and you would have to use more `if` for this. – furas Oct 12 '19 at 12:52

3 Answers3

0

It's not python who knows that's a tuple! you should handle it if you want python to presume that coor1 and coor2 are tuples. now if you create an object of your class like :

line1 = Line("hi","Sorath")

coor1 would be equal to hi and coor2 would be equal to Sorath and both of them are strings.

coor1 and coor2 can be everything, you should define the type of them while you are passing values or when you are writing __init__ !

def __init__(self,coor1,coor2):
    if type(coor1)==tuple:
        self.coor1=coor1  #tuple (x1,y1)
    else:
        self.coor1 = () #empty tuple

    if type(coor2)==tuple:
        self.coor2=coor2  #tuple (x2,y2)
    else:
        self.coor2 = () #empty tuple
Mohsen_Fatemi
  • 2,183
  • 2
  • 16
  • 25
0

__init__() is just the constructor of the class. This method is called when you create a new object.

def __init__(self, coor1, coor2): means that you want the two arguments (i.e. coor1 and coor2) to create the object. For example, when you create an object, you can do:

coor1 = (2, 6) # a tuple
coor2 = (-4, 12) # a tuple

l = Line(coor1, coor2) # create an object Line

Python is interpreted language and dynamically typed. So the types of variables are inferred by the interpreter.

To know the type of a variable, you can use the function type(object). For instance

type(coor1)
<class 'tuple'>
codrelphi
  • 1,075
  • 1
  • 7
  • 13
0

The following should give a good explanation on why: https://wiki.python.org/moin/Why%20is%20Python%20a%20dynamic%20language%20and%20also%20a%20strongly%20typed%20language

TLDR: the onus is on the developer to ensure the data type you're using is right for that function. Since Python is dynamically-typed, it would "know" what the data type would be upon value assignment.

Python 3.5+ though introduced the declaration of the variables' data type (i.e., type hinting):

def __init__(self, coord1: tuple, coord2: tuple):
    # initialize
jayg_code
  • 571
  • 8
  • 21