CS 108 8.1-8.9 Classes

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

class' interface.

part supposed to be visible to user and supposed to be edited etc.

Class methods

Functions that are also class attributes.

An instantiation operation is performed by

"calling" the class, using parentheses like a function call as in my_time = Time(). An instantiation operation creates an instance, which is an individual object of the given class. An instantiation operation automatically calls the __init__ method defined in the class definition

__init__

A constructor method that initializes a class instance.

Class object

A factory for creating new class instances.

class

A group of related variables and functions.

self

A method parameter that refers to the class instance.

attribute

A name following a "." symbol.

Class attribute

A variable shared with all instances of a class.

Instance attribute

A variable that exists in a single instance.

instance

An instantiation of a class.

operator overloading

Class customization can redefine the functionality when operators are used with class instances

Instance object

Represents a single instance of a class.

create a new instance of Widget with p1=15 and p2=5. class Widget: def __init__(self, p1, p2): # ... widg = ??????

Widget(15, 5)

isinstance() function returns

a True or False Boolean depending on whether a given variable matches a given type.

A method object is an attribute of

a class, and can be referenced using dot notation.

A class can be used to implement the computing concept known as an abstract data type (ADT), which is

a data type whose creation and update are constrained to specific, well-defined operations (the class interface). A key aspect of an ADT is that the internal implementation of the data and operations are hidden from the ADT user, a concept known as information hiding

A method is

a function defined within a class

A class attribute is shared amongst

all of the instances of that class EX: class ClassName: class_attribute = 'shared' def __init__(self): self. instance_attribute = 'unique'

The __init__ method has a single parameter "self", that

automatically references the instance being created. A programmer writes an expression such as self.hours = 5 within the __init__ method to create a new attribute hours

Write a statement that creates an instance of Animal class called "cat".

cat = Animal() __init__() has no parameters except self, thus Animal() creates an empty instance of Animal.

Write a statement that calls the noise method of the cat instance with the argument "meow". class Animal: def __init__(self): # ... def noise(self, sound): # ...

cat.noise(meow)

give default values to a class

class ClassName: def __init__(self, parameter, param2=default, param3=default2):

How to write a class

class ClassName: def__init__(self): self.attribute = value self.attribute2 = value2

Assignments within the class definition are

class attributes.

How to use the __str__ method

def __str__(self): return ("type %d print %f statement %s here" %(integer, float, string))

Class customization is the process of

defining how a class should behave for some common operations Ex: how something prints

Instance variables are assigned using

dot notation use this knowledge to decipher between instant attributes and class attributes

An instance attribute can be unique to each instance.

each instance. and are created using the . method EX: class ClassName: class_attribute = 'shared' def __init__(self): self.instance_attribute = 'unique and mutable'

A class object acts as a factory that creates

instance objects. When created by the class object, an instance object is initialized via the __init__ method

Mutators are

methods designed to allow change to private data

Accessors are

methods designed to provide access to private data

define a new variable using the Time class

my_time = Time()

The __init__ method, commonly known as a constructor, is

responsible for setting up the initial state of the new instance.

__init__ is a special method name, indicating

that the method implements some special behavior of the class.

attributes determine

the data and behavior of the class

A class interface consists of

the methods that a programmer calls to create, modify, or access a class instance.

can there be a default parameter anywhere in the class def

they must come last in the list of parameters of the __init__ funtion

how to reset or mutate the value of a calss

use def set_value(self, new_val): self._value = new_val *if* there is an invariant, enforce it here too! EX: denom can not be zero --> use if statement to ensure this

how to get a value out of a class

use def get_value(self): return self._value

how to change the way things a printed from a class

use the special method name __str__(self):


संबंधित स्टडी सेट्स

7.o. Kémia 6 - Az atom felépítése

View Set

PNU 133 PassPoint PrepU School-age Child

View Set

Strategic Management ch.12 Corporate Governance and Business Ethics

View Set

Nutrition Practice questions - Final

View Set

Accounting Exam 3 Chapter 8 study areas

View Set

8.1 Loss Grief Dying and End of Life Care

View Set