Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 231 Lab 11.

Similar presentations


Presentation on theme: "CSE 231 Lab 11."— Presentation transcript:

1 CSE 231 Lab 11

2 Topics to Cover Today Classes Creating classes
Attributes and Methods in class Instantiate class

3 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.

4 Classes

5 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

6 Classes – Creating a Class
# here is the bare minimum you need to make a class in Python class ClassName(): pass

7 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.

8 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'

9 Classes – What is __init__?
class SomeClass(): def __init__(self, name): self.name = name print(“Hi”, self.name) >>> x = SomeClass(‘Bob’) # good Hi Bob

10 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

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

12 What is the purpose of this method?
def __str__ (self): out_str = “{},{}”.format(self._a,self._b) return out_str

13 Classes – Making a Method
class SomeClass(): def __init__(self, number): self.number = number # member variable/attribute def get_square_number(self): return self.number ** 2 >>> x = SomeClass(2) >>> x. get_square_number() 4

14 Classes – What is self? class SomeClass(): def __init__(self, number): self.number = number def square(self): print(self.number**2) # <- self gets replaced with John! >>> John = SomeClass(5) >>> John. square() 25 >>> Bob = SomeClass(6) >>> Bob. square() 36

15 Classes – What is self? NameError
class SomeClass(): def __init__(self, number): self.number = number def print_number(self): print(number) >>> Bob = SomeClass(6) >>> Bob.print_number() NameError # you forgot to mention self

16 Classes – What is self? class SomeClass(): def __init__(self, number): self.number = number def print_number(self): print(self.number) >>> Bob = SomeClass(6) >>> Bob.print_number() 6


Download ppt "CSE 231 Lab 11."

Similar presentations


Ads by Google