# Python Classes and Inheritance_2

Python Classes and Inheritance_2

一、Exception

1. Adding Parameters to the Constructor

Exceptions provide us with way way to have a non-sequential point where we can handle something out of the ordinary (exceptional). So if there are errors appeared into your program, using exception method can avoid determine your program and inplement the code in exception module. It can be used in testing code.
Some example:

class Point:
    """ Point class for representing and manipulating x,y coordinates. """

    def __init__(self, initX, initY):

        self.x = initX
        self.y = initY

p = Point(7,6)

2.Adding other function to class

First, we should know what the class is.In my own opinion, class is a independent program module which is nearly the same as main program, just like classes in C#.
So, defining different functions in class should has a special label to show that it can invoke the parameter belong to this class.‘self’ can do this, just like code should below.

try:
    items = ['a', 'b']
    third = items[2]
    print("This won't print")
except Exception:
    print("got an error")

print("continuing")

And if you only want to get some kinds of errors, exception can be changed into specific kind of errors, such as IndexError…
some examples:

try:
   items = ['a', 'b']
   third = items[2]
   print("This won't print")
except Index:
   print("got an error")

print("continuing")

 And how can we understand the line that it determine from? We can treat it as if-else method. Since we reach the place that error appear, it'll drop out soon and inplement the code in exception.

If we want to analyze which kind of error it is, we can handle it like this.

try:
   items = ['a', 'b']
   third = items[2]
   print("This won't print")
except IndexError:
   print("got an error")
except TypeError:
   print("sss")
print("continuing")

This is a common thing to do in the init method for a class: take in some parameters and save them as instance variables.

So in this way, a useful function which can be define in our own class only one time is constructor.Constructor is defined with a method called ‘init’,just like the instance below.

class Point:
    """ Point class for representing and manipulating x,y coordinates. """

    def __init__(self, initX, initY):

        self.x = initX
        self.y = initY

p = Point(7,6)

2.Adding other function to class

First, we should know what the class is.In my own opinion, class is a independent program module which is nearly the same as main program, just like classes in C#.
So, defining different functions in class should has a special label to show that it can invoke the parameter belong to this class.‘self’ can do this, just like code should below.

class Point:
    """ Point class for representing and manipulating x,y coordinates. """

    def __init__(self, initX, initY):

        self.x = initX
        self.y = initY

    def getX(self):
        return self.x

    def getY(self):
        return self.y

    def distanceFromOrigin(self):
        return ((self.x ** 2) + (self.y ** 2)) ** 0.5

tips:
class is just a kind of data objects, so it can also be evaluated to a param, and it can also be given to a function as a input param.

3. Comprehension with ‘self’

Self can be treated as a bridge between both inner class and outer class, if a param want to be invoked outside the class, it should be ‘public’ just like self expressed.And if a param can be used in different function of a class,it should bee also define by self.

tips:so what can be program independently instantiate a class is the function has double underscore such as “__ init __”.

二、使用步骤

1.引入库

代码如下(示例):

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
import  ssl
ssl._create_default_https_context = ssl._create_unverified_context

2.读入数据

代码如下(示例):

data = pd.read_csv(
    'https://labfile.oss.aliyuncs.com/courses/1283/adult.data.csv')
print(data.head())
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值