python学习-62 类属性的增 删 该 查
类属性
1.类属性
类属性又称为静态变量,或者是静态数据。这些数据是与它们所属的类对象绑定的,不依赖于任何类实例。
2.增 删 改 查
class zoo:
country = 'china'
def __init__(self,name,address,kind):
self.name = name
self.address = address
self.kind = kind
def monkey(self):
print('this is monkey (%s)' %self.address)
def tiger(self):
print('this is tiger (%s)' %self.kind)
def end(self):
print('welcome to %s' %self.name)
dwy = zoo('chinese\'s zoo','beijing','others') zoo.monkey(dwy)
zoo.tiger(dwy)
zoo.end(dwy) # 对类的数据属性增 删 改 查 print(zoo.country) # 查看信息 zoo.country = 'US' # 修改信息
print(zoo.country) zoo.snake = 'this is snake' # 增加
print(dwy.snake) del zoo.country # 删除
del zoo.snake print(zoo.__dict__)
运行结果:
this is monkey (beijing)
this is tiger (others)
welcome to chinese's zoo
china
US
this is snake
{'__module__': '__main__', '__init__': <function zoo.__init__ at 0x7fb30efb8d90>, 'monkey': <function zoo.monkey at 0x7fb30efb8f28>, 'tiger': <function zoo.tiger at 0x7fb30efc9048>, 'end': <function zoo.end at 0x7fb30efc9378>, '__dict__': <attribute '__dict__' of 'zoo' objects>, '__weakref__': <attribute '__weakref__' of 'zoo' objects>, '__doc__': None} Process finished with exit code 0
3.
class zoo:
country = 'china'
def __init__(self,name,address,kind):
self.name = name
self.address = address
self.kind = kind
def monkey(self):
print('this is monkey (%s)' %self.address)
def tiger(self):
print('this is tiger (%s)' %self.kind)
def end(self):
print('welcome to %s' %self.name)
dwy = zoo('chinese\'s zoo','beijing','others') # 调用
zoo.monkey(dwy)
zoo.tiger(dwy)
zoo.end(dwy) # 类的函数属性的增加 def eat(self,food):
print('%s 正在吃%s'%(self.name,food))
zoo.eat = eat
print(zoo.__dict__) # 以列表的方式 查看是否添加进去了 dwy.eat('草')
运行结果:
this is monkey (beijing)
this is tiger (others)
welcome to chinese's zoo
{'__module__': '__main__', 'country': 'china', '__init__': <function zoo.__init__ at 0x7f90cdc3ed90>, 'monkey': <function zoo.monkey at 0x7f90cdc3ef28>, 'tiger': <function zoo.tiger at 0x7f90cdc4f048>, 'end': <function zoo.end at 0x7f90cdc4f378>, '__dict__': <attribute '__dict__' of 'zoo' objects>, '__weakref__': <attribute '__weakref__' of 'zoo' objects>, '__doc__': None, 'eat': <function eat at 0x7f90cdd49268>}
chinese's zoo 正在吃草 Process finished with exit code 0
python学习-62 类属性的增 删 该 查的更多相关文章
- Python学习:类和实例
Python学习:类和实例 本文作者: 玄魂工作室--热热的蚂蚁 类,在学习面向对象我们可以把类当成一种规范,这个思想就我个人的体会,感觉很重要,除了封装的功能外,类作为一种规范,我们自己可以定制的规 ...
- python之类与对象属性的增删改查
类属性与对象属性的增删改查 类属性的增删改查 class School: """ 文档 """ Teacher = "老王&quo ...
- Python内置类属性,元类研究
Python内置类属性 我觉得一切都是对象,对象和元类对象,类对象其实都是一样的,我在最后进行了证明,但是只能证明一半,最后由于元类的父类是type,他可以阻挡对object属性的访问,告终 __di ...
- Python内置类属性
__dict__ : 类的属性(包含一个字典,由类的数据属性组成) __doc__ :类的文档字符串 __name__: 类名 __module__: 类定义所在的模块(类的全名是'__main__. ...
- 好用的SQL TVP~~独家赠送[增-删-改-查]的例子
以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化. 本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...
- iOS FMDB的使用(增,删,改,查,sqlite存取图片)
iOS FMDB的使用(增,删,改,查,sqlite存取图片) 在上一篇博客我对sqlite的基本使用进行了详细介绍... 但是在实际开发中原生使用的频率是很少的... 这篇博客我将会较全面的介绍FM ...
- iOS sqlite3 的基本使用(增 删 改 查)
iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...
- django ajax增 删 改 查
具于django ajax实现增 删 改 查功能 代码示例: 代码: urls.py from django.conf.urls import url from django.contrib impo ...
- Python学习---django之ORM的增删改查180125
模型常用的字段类型参数 <1> CharField #字符串字段, 用于较短的字符串. #CharField 要求必须有一个参数 maxlength, 用于从数 ...
随机推荐
- python -- 连接 orclae cx_Oracle的使用 二
转:https://www.cnblogs.com/cyxiaer/p/9396861.html 必需的Oracle链接库的下载地址:https://www.oracle.com/technetwor ...
- tensorflow2.0 学习(三)
用tensorflow2.0 版回顾了一下mnist的学习 代码如下,感觉这个版本下的mnist学习更简洁,更方便 关于tensorflow的基础知识,这里就不更新了,用到什么就到网上取搜索相关的知识 ...
- Greenplum 6 新功能 在线扩容工具GPExpand (转载)
Gpexpand是Greenplum数据库的扩容工具,可以为集群增加新节点从而可以存储更多的数据,提供更高的计算能力.Greenplum 5及之前,集群扩容需要停机增加新节点,然后对表数据做重分布.因 ...
- 微信小程序弹窗
wxml <view class="content"> <button bindtap="popSuccessTest">成功提示弹窗& ...
- 洛谷 P2983 [USACO10FEB]购买巧克力Chocolate Buying 题解
P2983 [USACO10FEB]购买巧克力Chocolate Buying 题目描述 Bessie and the herd love chocolate so Farmer John is bu ...
- CSP-S乱搞记
还有一年的时间,没人能挡住我前进的脚步 以后不打算写游记了,补完这篇再写就等退役吧,不太想传播什么负能量,走这条路,希望能得到自己想要的东西 Day-n 上了一个月文化课,班主任突然催我搞竞赛??? ...
- docker_基础用法
1. docker architecture 2. 命令
- 解决idea创建Maven项目速度慢
idea在创建maven项目的时候会去网上自动下载需要的插件,这样就会导致项目创建后一直处于下载插件的状态中,影响开发效率 此时我们可以在创建maven骨架的时候,加入键值对来让maven调用本地的骨 ...
- 运行OpenGL红宝书第9版源码时Visual Studio提示“无法启动程序...ALL_BUILD。拒绝访问“的问题的解决办法
问题描述: OpenGL红宝书第9版源码采用CMake编译后,用相应的Visual Studio(如VS2012)打开“vermilion9.sln”解决方案,并运行时Visual Studio提示“ ...
- 微信小程序的跳转navigateTo()和redirectTo()用法和区别
原文链接:https://blog.csdn.net/u013128651/article/details/79736410 wx.navigateTo({}) ,保留当前页面,跳转到应用内的某个页 ...