python基础-命名空间、类_对象调用范围、对象组合

来源:互联网 发布:木村拓哉 知乎 编辑:程序博客网 时间:2024/06/06 23:19

        • 面向对象的命名空间
        • 可变不可变类型
        • 类对象调用方法的示例
        • _dict_
        • 不能使用global
        • 类获取对象属性
        • 对象组合

面向对象的命名空间

1、类名操作可变、不可变数据类型,都是类中对应的变量发生变化

2、对象名操作静态变量
2_1引用变量:在自己的命名空间中查找,找不到就去类的命名空间找

3、修改变量:
3_1如果是对可变数据类型中的元素进行修改,那么全局生效
3_2如果是对变量进行重新赋值,那么只是在对象自己的命名空间里增加了一个新的属性
结论:应该尽量用类名去使用静态变量

class Foo:    country = 'China'    country_lst = ['China']    def __init__(self,name):        self.name = nameegg = Foo('egon')safly = Foo('safly')print(safly.country,safly.country_lst)safly.age = 90safly.country_lst = []safly.country_lst.append('印度')print(safly.age,safly.country,safly.country_lst)print(Foo.country,Foo.country_lst)print(egg.country,egg.country_lst)

输出如下:

E:\python\python_sdk\python.exe E:/python/py_pro/3.面向对象的命名空间.pyChina ['China']90 China ['印度']China ['China']China ['China']Process finished with exit code 0

可变、不可变类型

class Foo:    country = 'China'    country_lst = ['China']    def __init__(self,name,country):        self.name = name        self.country = countryalex = Foo('alexander',"印度")print(alex.country)#删除对象属性,会查询类属性del alex.countryprint(alex.country)print(alex.country_lst)alex.country_lst.append('印度')print(alex.country_lst)egg = Foo('alexander',"印度")print(egg.country_lst)print(Foo.country_lst)

输出如下:

E:\python\python_sdk\python.exe E:/python/py_pro/3.面向对象的命名空间.py印度China['China']['China', '印度']['China', '印度']['China', '印度']Process finished with exit code 0

类、对象调用方法的示例

def method():    print("out method")class Animal():    name = "animal"    method = method    def method1(self, a):        print("method1",a)    def method2(a):        print("method2", a)a = Animal()print(a.method1(1))print("--------")#如下方式不行# a.method()#如下方式也不行# a.method2(2)#如下可以调用类外函数Animal.method()print("--------")Animal.method2(111)print("--------")Animal.method1(Animal,333)print("--------")Animal.method1(a,333)

输出如下:

E:\python\python_sdk\python.exe E:/python/py_pro/python.pymethod1 1None--------out method--------method2 111--------method1 333--------method1 333Process finished with exit code 0

_dict_

init方法可以不写

#init可以不写class Person:    def eat(self):        passalex = Person()alex.eat()print(alex.__dict__)

输出如下:

E:\python\python_sdk\python.exe E:/python/py_pro/python.py{}Process finished with exit code 0

不能使用global

这里写图片描述

我做了几个测试,是无法使用global的

类获取对象属性

#Person不能直接获取对象属性#但是可以通过方法间接获取class Person:    name = "saf"    def __init__(self,name):        self.name = name    #获取对象属性    def getName(self):        return self.namep1 = Person("safly1")#获取对象属性print(Person.getName(p1))

输出如下:

E:\python\python_sdk\python.exe E:/python/py_pro/python.pysafly1Process finished with exit code 0

对象组合

一个对象的属性是另外一个类的对象

class Teacher:    def __init__(self,name,age,sex,year,month,day):        self.name = name        self.age = age        self.sex = sex        self.birth = Birthday(year,month,day)class Birthday:    def __init__(self,year,month,day):        self.year = year        self.month = month        self.day = dayboss_gold = Teacher('太亮',40,'不详',1968,12,31)print(boss_gold.birth.year)print(boss_gold.birth.month)print(boss_gold.birth.day)

输出如下:

E:\python\python_sdk\python.exe E:/python/py_pro/4.组合.py19681231Process finished with exit code 0
原创粉丝点击