So I understand that classes are basically blueprints for when you are coding, but the thing I don't understand is the difference they have from functions. Since for both of them you can assign variables and use different variables inside of them. And in order for both of them to work you have to use exampleclass() or examplefunction() for it to execute. Also as well you can execute a function inside a function or define a function within a functions and with a class you can execute or define a class inside a class.
example class:
class bank(object):
def __init__(self, acc_num, acc_name, acc_DOB):
self.acc_num = acc_num
self.acc_name = acc_name
self.acc_DOB = acc_DOB
or example function:
def bank(num, name, DOB):
acc_num = num
acc_name = name
acc_DOB = DOB
*The two example I have given may not be perfectly correct but I was doing off the top of my head.
The two examples I have given look exactly the same but and look like they do the same thing so what is the difference.
CodePudding user response:
Functions do specific things, classes are specific things.
Classes are a blueprint, like you said. A class can have methods inside of them, and in your example, you are showing the constructor in action. That method, when executed, creates a new object using that blueprint. Compared to the function you listed in your example, which creates local variables and sets them equal to the passed in parameters. Also, you can't "execute" a class. You can only call methods in it. Ex) class.method() compared to fiction().