CS 521 Module 6

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

subclass

inherits data fields and methods from the superclass, but can have many other data fields and methods

class Circle: def __init__(self, radius = 1): self.radius = radius

init function

class provides a special method ____init___(), this method is known as an _______. it is invoked to initialize a new objects state when it's created. initializers are designed to perform initializing actions, such as creating an objects data fields with initial value.

initializer

the scope of an ______ is the entire class once its created

instance variable

if you take out str() method _____

it will call repr() method and output is literal representation

UML: datafieldName: datafieldtype classname(parametername: parametertype) methodname(parametername: parametertype): return Type

layout of UML

instance variables

local to each instance

call by reference copies _______

location

objects provide _____ to operate on _____ attributes

methods, data attributes

each class in an association may have _____, which could be a number or interval that specifies how many of the classes objects are involved in the relationship.

multiplicity

when passing a ____ object into a function, the contents of the object may change

mutable

__module__

name of class module

oop involves the use of objects to create programs

object oriented programming

You can access the objects data fields and invoke its methods by using the "dot operator"

objectReferenceVariable.datafield objectReferenceVariable.methods(args) from Circle import Circle c = Circle(5) c.radius output 5

main diff between overloading and overriding

overloading we can use same function name with diff parameters for multiple times for different tasks within a class

**kwargs x_dict = {"a": 10, "b": 5} f,g = add(**x_dict)

passing arguments as a dictionary

circle = Circle(1,5) print("A circle ", circle) is the same as

print("A circle", circle.__str__()) str displays the color and filled properties

2nd way) try/except block def ratio(a,b): try: c = a/b return c except exception as e: print(e)

prints the error that comes from python

- dash in a UML class diagram indicates a ____

private data field or method of the class

get method

provides the user a way to return a data field

set method

provides the user a way to set a new value

recursive functions

require context switching and takes a long time because of lots of overhead. allocates lots of memory each time its called

__eq__(other)

returns True if two objects are the same. x.__eq__(y) is false because they are different objects

__str__() method

returns a string description for an object

when an object is initiated, it is passed to the ______ parameter

self class Circle: pi = 3.14 def __init__(self, r = 1): self.radius = r circle1 = Circle(5) circle2 = Circle(10)

all functions including the init function have the first parameter as _____. This parameter refers to the object that invokes the method.

self the self parameter in the init method is automatically set to reference the object that was just created

abstraction

separates class implementation from the use of a class

Aggregation

special form of association that represents an ownership relationship between two objects.

two ways to deal with errors: - assert statement: def ratio(a,b): assert (b != 0), 'Cannot divide by zero' return a/b

specifies custom error message if assert statement not satisfied

using composition you can create a list as a dat field in the ____ class

stack. this is better because it enables you to define a completely new stack without inheriting the unnecessary and inappropriate methods of the list class

an objects ____ is represented by variables, called data fields. a circle object has a data field, radius, which is a property that characterizes a circle. A rectangle has data fields width and height.

state (aka properties, attributes)

class Circle(): pi = 3.14 can do Circle.pi = 314 and change the _______

static variable

underscores

1) If_ can make a keyword a second 2) _x class variable is private but can still be accessed when called directly 3) __x mangles the name, cant access directly 4)__init__ internal functions within a class

Underscore holds: _

1) result of last computation. 2) _ for variables you don't need, if function returns many results and you only want some of them 3) trailing underscore: x_ = 9 if _ = 9 4) for readability of numbers, can do x = 3_100_589 5) in classes, underscore is to make variable private 6) 2 underscores in front of a function ___add__() these are special functions for classes to implement particular functionality

Overriding

2 methods with the same name but methods have different functionalities

overloading

The process of creating multiple methods of the same name but different return types

classes implement _____

abstraction

a python class uses variables to store data fields and defines methods to perform _____

actions

other option if you dont need to use it again: print("Area is", Circle(5).getArea())

anonymous object

static variables

are shared across all instances. they are made between class def and constructor.

static variables

are shared, single copy, and are defined before class methods class Circle: pi = 3.14

class Circle(): pi = 3.14 if you do __pi = 3.14 and then down below, Circle.__pi = 314, it will create a new variable called __pi = 314

at the beginning, it creates a new variable so it cant be modified

__new__()

automatically invoked when an object is constructed. This method invokes the init object

map function

avoids using iterations, accomplishes same thing though. can operate on collections. applies a call function for each item

python uses methods to define an objects ______. methods are defined as functions. you make an object perform an action by invoking a method on that object. you can define methods named getArea() and getPerimeter() for circle objects.

behavior

in order to access an objects data fields and invoke an objects methods, you need to assign the object to a variable by using the following syntax

c1 = Circle(5) c2 = Circle()

calling a superclass class Circle(GeometricObject): def __init__(self, radius): super().__init__() #superclass initializer self.__radius = radius

calling a superclass

isinstance()

can be used to determine whether an object is an instance of class uses syntax: isinstance(object, Classname)

a private method ____ be overwritten. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated.

cannot

even if you make a variable private, if the user puts in the right thing can still _______. not 100% protection

change it

class methods use dot notation

circle = Circle(5) area = circle.area()

when passing an _____ object into a function, the object will not be changed

immutable

__doc__

class documentation, displays docstring

__name__

class name

__dict__

class name space

objects are created from ______

classes

__bases__

classes from which this class inherits

__repr__()

computes "official" representation of an object repr(circle) output: '<__main__.Circle instance at 0x00000000000DB7F549>

constructor

create objects from the class 1) creates an object in the memory for the class 2) invokes the classes init method to initialize the object

subclasses ____ or override superclass methods

inherit

inheritance

defining new classes from existing classes. general class (superclass) to specific class (subclass)

multiple inheritance

derive a subclass from several classes To define a subclass from multiple classes use the syntax: class Subclass(Superclass1, Superclass2):

use dir() command to see all the methods and variables associated with a class

dir(class)

object class

every class in python is descended from the object class

destructor: def ___del__(self):

executed upon deletion

non-static variables

exist in each class instance (multiple copies) class Circle: self.pi = 3.14 #not static

interrupts are _____ to the code

external to the code. network connection down, database crashed.

Association

general binary relationship that describes an activity between 2 classes.

__str__()

gives "human" representation of an object str(circle) output: circle r = 1 uses repr as a fallback

an objects ______ is like a persons social security number. python automatically assigns each object a unique ___ for identifying the objects at runtime.

identity id

__str__()

supports printing line when you print a class c2 = Circle() print(c2) allows you to print it because of the str method

displayObject()

takes a parameter of the geometric object type (example: Circle(4))

encapsulation

the implementation are invisible to the user. The functions and data are combined into objects

__str__ method is defined in both Geometric Objects and Circle classes, which one is displayed?

the one from Circle classes. It does by the first instance of the implementation and goes backwards from specific class to general class until it finds what its looking for.

class Rectangle: def __init__(self, width = 1, height = 1): super().__init__()

this calls the superclass init method which is necessary to create data fields defined in the superclass

accessor/getter def get_radius(self): return self.radius

this is a accessor/getter it returns values without changes

constructor def __init__(self, r=1): self.radius = r

this is a constructor with a default

mutator/setter def set_radius(self, r): self.radius = r

this is a mutator, a setter this can set or change values

UML

unified modeling language

Private variables

use ___ to make private def __init__(self, r = 1): self.__radius = r now cannot change variables

private methods

used for object's inner workings, to keep it a secret from the user.

tuples are used to ______ parameters to functions

used to pass parameters results are returned in a tuple if multiple things asked.

stack process

when a function is invoked, an activation record is created that stores its parameters and local variables are pushed into the stack.


Kaugnay na mga set ng pag-aaral

Chapter 8 - Corporate Diversification

View Set

Chapter 4 Using Supply and Demand

View Set

fin exam 2-hw and practice exams

View Set

MGMT 417 Campolongo Test 2 Chapter 5

View Set

Chapter 11 | Properties of the Hair and Scalp

View Set

Chapter 18: Business Organizations and Employment Law

View Set