160 likes | 170 Views
Learn about creating and instantiating classes in Python, including attributes, methods, and the importance of the __init__ method. Understand the concept of self in classes and how to define and use member variables.
E N D
Topics to Cover Today • Classes • Creating classes • Attributes and Methods in class • Instantiate class
Classes • You’ve been working with a bunch of different classes all semester: list, set, dict, etc. • A class outlines how to “make” something and what that something “is.” • Classes are an Object Oriented Programming (OOP) paradigm.
Class and Instance D = set({1,2,3,4}) Creates an instance from set class set() is the constructor of the class set • Class is like a cookie cutter • Instance is like a cookie
Classes – Creating a Class # here is the bare minimumyou need to make a class in Python class ClassName(): pass
What is the purpose of this method? def __init__(self, A = 0, B = 1): self.__a = A self.__b = B • The moment you ask for an instance of a class to be created, the __init__ function is called. • So if you need to setup anything for an object, • it should go in init.
Classes – What is __init__? Arguments you define for __init__ (besides self) are what you pass when you create an object. class SomeClass(): def __init__(self, name): self.name = name # instances have a member called name print(“HI ”, self.name) • >>> x = SomeClass() # ERROR • Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: __init__() missing 1 required positional argument: 'name'
Classes – What is __init__? class SomeClass(): def __init__(self, name): self.name = name print(“Hi”, self.name) >>> x = SomeClass(‘Bob’) # good Hi Bob
Classes – What is __init__? class SomeClass(): def __init__(self, name = ‘Bob’ ): self.name = name print(“Hi”, self.name) >>> x = SomeClass() • # good >>> x = SomeClass(‘ZACH’) • # good • Hi Bob • Hi ZACH
What happens? class Time: def __init__(self, hour = 0, minute = 0, second = 0): print(hour,minute,second) What are the values of hour, minute and second if I create: C = Time(50,7) 50 7 0 C = Time(second = 83, hour = 24)) 24 0 83
What is the purpose of this method? def __str__ (self): out_str = “{},{}”.format(self._a,self._b) return out_str
Classes – Making a Method class SomeClass(): def __init__(self, number): self.number = number # member variable/attribute defget_square_number(self): return self.number ** 2 • >>> x = SomeClass(2) • >>> x. get_square_number() • 4
Classes – What is self? class SomeClass(): def __init__(self, number): self.number = number defsquare(self): print(self.number**2) # <- self gets replaced with John! • >>> John = SomeClass(5) • >>> John. square() 25 • >>> Bob = SomeClass(6) • >>> Bob. square() 36
Classes – What is self? class SomeClass(): def __init__(self, number): self.number = number defprint_number(self): print(number) • >>> Bob = SomeClass(6) • >>> Bob.print_number() NameError • # you forgot to mention self
Classes – What is self? class SomeClass(): def __init__(self, number): self.number = number defprint_number(self): print(self.number) • >>> Bob = SomeClass(6) • >>> Bob.print_number() 6