类与对象class(oop)
web flask
运维 salt zabbix cobbler
大数据 hadoop
类的属性 类里面的变量,实例化后可以修改
类的方法 类里面的函数
类的构造器,解构器
def __init__(self):
在实例初始化是隐藏调用
def __del__(self):
在实例消亡时隐藏调用实例化 f1 = Friut('app',33)
self表示实例化的名字
基类 包含 子类 包含 对象
类和函数都是可调用的
实现一个计数器,统计多少的实例
class Friut(object): 定义类
count = 0 类的一个属性
def __init__(self,name,weight): 初始类的实例的属性
self.name = name
self.weight = weight
Friut.count += 1
def info(self): 类包含的一个的对象或方法
print self.name,self.weight,Friut.count
类的方法
@staticmethod
def func_name()
他的调用只能通过类名.func_name()
@classmethod
def func_name(self)
调用方式类名.func_name() 或者 实例名.func_name()
通过dir(类名)查看
__doc__定义类时的描述
__name__类的名字
__module__ 模块的名称 __main__
__base__类的基类
__dict__所有类的方法和属性
类的继承、封装
class Parent(object): # define parent class
def myMethod(self):
print 'Calling parent method'
def setAttr(self, attr):
Parent.parentAttr = attr
class Child(Parent): # define child class 继承Parent
def myMethod (self):
print 'Calling child method' 覆盖父类的myMethod方法
隐藏类的方法
class JustCounter(object):
__secretCount = 0
def count(self):
self.__secretCount += 1
print self.__secretCount
counter = JustCounter()
counter.count()
print counter.__secretCount
实例化后无法直接调用,只能由class调用
super
class A(object):
def __init__(self):
print "enter A"
class B(A): #
def __init__(self):
print "enter B"
super(B, self).__init__()
print "leave B"
变量范围
B 内建build in:没有定义过,拿来就能用的
G 全局 global
E 闭包
L 本地变量 local
模块
import 模块或者py文件
在以下位置寻找
import sys
print sys.path