Docsity

Programacion I: programacion orientada a objetos en Python (UFV), Diapositivas de Programación Informática

Vista previa parcial del texto

¡Descarga Programacion I: programacion orientada a objetos en Python (UFV) y más Diapositivas en PDF de Programación Informática solo en Docsity!

Last updated on 28/07/2020 09:

Business Analytics

PROGRAMACIÓN I

OBJECT ORIENTED PROGRAMMING

‣ Classes and instances

‣ Class side data and methods

‣ Static methods

‣ Best practices

OBJECT ORIENTED PROGRAMMING

1. Title

Object Oriented Programming (or OOP): programming paradigm (pattern, model)

Approach to structuring programs/applications so that the data held, and the operations performed

on that data, are bundled together into classes and accessed via objects.

Classes can be used to represent real world entities (an employee, a product, a vehicle), but also

abstract (financial transaction, a purchase order).

https://docs.python.org/3/tutorial/classes.html Object Oriented Programming

1. Title

Classes as templates used to constructed instances or examples of a class of things.

Instances have same data structure (attributes) but contain their own values.

A class should accomplish one specific purpose; it should capture only one idea.

Classes and instances

1. Title

class NameOfClass:

Class definitions, like function definitions (def statements) must be executed before ”use"

In practice, the statements inside a class definition will usually be function definitions (that we will

call methods), but other statements are allowed, and sometimes useful

A class can be a subclass of another class (superclass), but we will see that later on.

Class definition

1. Title

Class instantiation uses function notation. Just pretend that the class object is a function that returns

a new instance of the class.

For example:

Here we (1) define a class called MyClass, with a single method f. We then (2) create an instance of

MyClass named x, and finally (3) we call the f method of the instance x

Basic instantiation class MyClass: """A simple example class""" def f(self): return 'hello world' x = MyClass() x.f()

1. Title

The instantiation operation (“calling” a class object) creates an empty object.

Many classes like to create objects with instances customized to a specific initial state.

For this purpose, we define a special method named init(), like this:

Instantiation with init (constructor) class Complex: def init(self, realpart, imagpart): self.r = realpart self.i = imagpart x = Complex(3.0, - 4.5) print(x.r, x.i)

1. Title

When a class defines an init() method, class instantiation automatically invokes init() for the

newly-created class instance.

Note that this init() method has two parameters realpart and imagpart. During instantiation,

we are passing the arguments 3.0 and - 4.5, so the object x will be created with an initial state (values

for the instance variables)

Instantiation with init (constructor) class Complex: def init(self, realpart, imagpart): self.r = realpart self.i = imagpart x = Complex(3.0, - 4.5) print(x.r, x.i)

1. Title

It is also possible to update an attribute after instantiation

Careful, this way it is possible to create a new attribute. x.w = 3.9 will result in an object x with three

attributes (r, i and w).

Accessing attributes x = Complex(3.0, - 4.5) x.i = 5.

1. Title

Remember that we create classes to bundle together data and operations performed on that data

Methods are the “functions inside classes”, typically performing operations on the object

The special thing about methods is that the instance object is passed as the first argument of the

function.

x.f() is exactly equivalent to MyClass.f(x).

You can have more parameters in the method, but the first one is a pointer to the instance, and the

convention is to use the name self

Methods, and self

1. Title

Two facts of life: dogs have names, dogs play tricks:

1. Create a class Dog, with two attributes, name for a string name, and tricks, a list to hold the

tricks a particular dog can play.

2. Add a method to the class that allows the update of a Dog object, incorporating a new trick (a

string description)

Dogs that play tricks

1. Simple example Title class Dog: def init(self, name): self.name = name self.tricks = [] # creates a new empty list for each dog def add_trick(self, trick): self.tricks.append(trick) d = Dog( 'Fido' ) e = Dog( 'Buddy' ) d.add_trick( 'roll over' ) e.add_trick( 'play dead' ) print(d.tricks)

['roll over']

print(e.tricks)

['play dead']

1. Title

Notes:

1. Hours and integers (0 to 24) (There is a library, datetime that is used to create and manage time

objects).

2. Use Mon, Tue, Wed, Thu, Fri, Sat, Sun to identify the name of the week

3. Docstrings for all methods.

4. Create an instance and confirm that the class is behaving as expected

5. Upload your results in CANVAS

Vehicle Routing

1. Title

Method str

  • A string that is informative for the user of the class
  • Will typically contain data from attributes.
  • For printing the object (consider the output for a log). print(user_1)

Method repr

  • Defines the string representing a Python expression, using which you can re-create the object.
  • That is eval()
  • repr(user_1) String representations