Python基础笔记(五)

1. 类(class)

下面的代码创建了一个Employee类:python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

class Employee(object):
    
    company = "IBM"

    def __init__(self, name, sex, age, salary):
        self.name = name
        self.sex = sex
        self.age = age
        self.__salary = salary

    def getSignature(self):
        signature = "My name is %s, I'm %d years old." % (self.name, self.age)
        return signature

    def getSalary(self):
        return self.__salary

    def setSalary(self, salary):
        if 0 < salary <= 10000:
            self.__salary = salary
        else:
            raise ValueError("Invalid Value")

tom = Employee("tom", "male", 23, 3000)

print(tom.getSignature())
# My name is tom, I'm 23 years old.

print(tom.age)
# 23

tom.setSalary(5000)
tom.__salary = 9000 # 无效,实际上是新增了一个名为"__salary"的变量
print(tom.getSalary())
# 5000

__init__方法至关于其它语言的“构造函数”,该方法的第一个参数必须为self,self表明建立的实例自己,所以在__init__方法内部能够把各类属性绑定到self;在实际调用时,self并不须要传递,Python解释器本身会把实例变量传进去。
以一个下划线开头的变量名,例如_company,这种变量外部是能够访问的,可是按照约定俗成的规定,这种变量应该视为私有变量,不要随意访问。
以两个下划线开头的变量名,例如__salary,是私有变量,外部不能直接访问,通常提供"get"和"set"方法去间接获取和修改。
开头与结尾都是两个下划线,例如__name__,是特殊变量,特殊变量是能够直接访问的。
须要注意的是,当在一个外部类尝试用下面的代码访问新建的Employee类时,是会报错的:函数

import Employee

tom = Employee("tom", "female", "23")

报错内容为TypeError: 'module' object is not callable,这个错误是指试图把模块做为一个函数来调用。产生错误的缘由是,import Emplyee实际上是导入了整个的Employee.py,而不是名为Employee的类。正确的作法有两种:ui

(1) 用“模块名.类名“来访问:code

import Employee

tom = Employee.Employee("tom", "male", 23, 6000)

(2) 用"from...import..."的形式导入对象

from Employee import *

tom = Employee("tom", "male", 23, 6000)

2. 获取对象的类型

type(obj)函数返回参数的对象类型,基本类型如intstr也能够用它来判断:utf-8

from Employee import *

tom = Employee("tom", "male", 23, 6000)

print(type(23))
# <class 'int'>

print(type("ABC"))
# <class 'str'>

print(type(tom))
# <class 'Employee.Employee'>

if type(123) == type(456):
    print("Equal")

if type("ABC") == str:
    print("Equal")

print(type(tom) == Employee)
# True

可使用types模块中定义的常量来判断一个对象是不是函数,lambda函数或generator:字符串

import types

def myFun():
    pass

# 是不是函数
print(type(myFun) == types.FunctionType)
# True

# 是不是内置函数
print(type(abs) == types.BuiltinFunctionType)
# True

# 是不是lambda函数
print(type(lambda x: x)==types.LambdaType)
# True

# 是不是generator
print(type((x for x in range(10)))==types.GeneratorType)
# True

能用type()判断的基本类型也能够用isinstance()判断:get

print(isinstance(23,int))
# True

print(isinstance("ABC", str))
# True

print(isinstance(b"A", bytes))
# True

print(isinstance(tom, Employee))
# True

isinstance()还能够用于判断一个对象是不是某些类型中的一种:generator

print(isinstance("23", (str, int)))
# True

print(isinstance([1, 2, 3], (list, tuple)))
# True

3. 获取对象的属性和方法

若是要得到一个对象的全部属性和方法,可使用dir(obj)函数,它返回一个包含字符串的listit

print(dir("ABC"))
# ['__add__', '__class__', ... , 'upper', 'zfill']

相似__xxx__的属性和方法在Python中都是有特殊用途的,好比__len__方法返回长度。在Python中,若是你调用len()函数试图获取一个对象的长度,实际上,在len()函数内部,它自动去调用该对象的__len__()方法,因此,下面的代码是等价的:

print(len("ABC"))

print("ABC".__len__())

下面的例子证实了len()会调用__len__()方法:

class MyClass1(object):
    def __len__(self):
        return 100

class MyClass2(object):
    pass

myClass = MyClass1()
print(len(myClass))
# 100

myClass = MyClass2()
print(len(myClass))
# TypeError: object of type 'MyClass2' has no len()

4. hasattr、getattr和setattr

利用这三个方法,能够判断对象是否有某属性/方法,获取指定名称的属性/方法,新增属性等操做:

class Employee(object):
    def __init__(self, name, sex, age, salary):
        self.name = name
        self.sex = sex
        self.age = age
        self.__salary = salary

    def getSignature(self):
        signature = "My name is %s, I'm %d years old." % (self.name, self.age)
        return signature

employee = Employee("tom", "male", 23, 3000)

# 判断对象是否有"age"属性,有则打印并赋值
if hasattr(employee, "age"):
    print(employee.age)
    employee.age = 18

# 若是对象没有"hobby"属性,则新增该属性并赋值
if not hasattr(employee, "hobby"):
    setattr(employee, "hobby", "music")

# 经过getattr获取对象指定的属性值
print(getattr(employee, "hobby"))
# music

# 若是试图获取不存在的属性,会抛出AttributeError的错误:
# getattr(employee, "gold")
# AttributeError: 'Employee' object has no attribute 'gold'

# 利用getattr的第三个参数:若是属性不存在,就返回一个默认值
print(getattr(employee, "gold", "not exist"))
# not exist

# 经过getattr获取方法,注意:若是方法不存在,会抛出AttributeError
print(getattr(employee, "getSignature"))
# <bound method Employee.getSalary of <__main__.Employee object at 0x10832a4a8>>

# 判断是否存在指定名称的方法,若是存在,则执行该方法
try:
    funcName = "getSignature"
    func = getattr(employee, funcName)
    if hasattr(func, "__call__"):
        print("存在方法", funcName)
        # 存在方法 getSignature
        print(func())
        # My name is tom, I'm 18 years old.
except AttributeError as e:
    print("没有方法:", funcName)
相关文章
相关标签/搜索