CS Chapter 11

Ace your homework & exams now with Quizwiz!

What is the difference between a method and a function?

A method and a function are closely related. They are both "small programs" that have parameters, perform some operation and (potentially) return a value. The main difference is that methods are functions tied to a particular object. When calling a function, func1(param1). When calling a method, object.func1(param1).

What happens when you don't provide a constructor?

If you don't provide a constructor, then only the default constructor is provided. The default constructor does system stuff to create the instance, nothing more. You cannot pass arguments to the default constructor.

What else makes methods different from functions?

Methods are defined inside the suite of a class. Methods always bind the first parameter in the definition to the object that called it. This parameter can be named anything, but traditionally it is named self.

How do you make a object-local value?

Once an object is made, the data is made the same way as in any other Python situation, by assignment. Any object can thus be augmented by adding a variable. For example, my_instance.attribute = 'hello'

What is the dir( ) function?

The dir() function lists all the attributes of a class.

What is the standard way to name class?

The standard way to name a class in Python is called CapWords in which each word of a class begins with a capital letter and there are no underlines.

What is important about self?

Through self we can access the instance that called the method (and all of its attributes as a result). When a dot method call is made, the object that called the method is automatically assigned to self. We can use self to remember, and therefore refer, to the calling object. To reference any part of the calling object, we must always precede it with self. The method can be written generically, dealing with calling objects through self.

What is dot reference?

We can refer to the attributes of an object by doing a dot reference, of the form: object.attribute. The attribute can be a variable or a function. It is part of the object, either directly or by that object being part of a class. For example, my_instance.my_val calls a variable associated with the object my_instance. my_instance.my_method() calls a method associated with the object my_instance.

Why do we create classes?

We make classes because we need more complicated, user-defined data types to construct instances we can use. The class has two aspects: the data (types, number, names, etc.) that each instance might contain and the messages that each instance can respond to.

What is the constructor?

When a class is defined, a function is made with the same name as the class. This function is called the constructor. By calling it, you can create an instance of the class. It is generally written as def __init__ (self, param1, param2, ...). By assigning values in the constructor, every instance will start out with the same variables. You can also pass arguments to a constructor through its __init__ method

What is __str__?

When print(my_inst) is called, it is assumed, by Python, to be a call to "convert the instance to a string", which is the __str__ method. In the method, my_inst is bound to self, and printing then occurs using that instance. __str__ must return a string.

What is the difference between a class and an instance of a class?

You define a class as a way to generate new instances of that class. Both the instances and the classes are themselves objects. The structure of an instance starts out the same, as dictated by the class. The instances respond to the messages defined as part of the class.

What is the basic format of a class?

class ClassName (object): with object being the parent object.


Related study sets

CIPP US Module 10 - Telecom and Marketing

View Set

LaCharity Chapter 13 Diabetes Mellitus

View Set

Regulatory Ethics, Agencies and Market Participants

View Set

Quiz 3: Pollution Affects Ecology

View Set