Python中的类学习笔记,Python类学习笔记
Python中的类学习笔记,Python类学习笔记
Python使用中面向对象的语言,支持继承、多态;
定义一个Person类:
>>> class Person:
... def sayHello(self):
... print('hello')
...
>>> Person.sayHello(None)
hello
>>> Person().sayHello()
hello
可以修改Person的类方法
>>> def hack_sayHello(obj):
... print('...hello')
...
>>>
>>> Person.sayHello = hack_sayHello
>>> Person.sayHello(None)
...hello
>>> Person().sayHello()
...hello
>>> sayHello = Person().sayHello
>>> sayHello()
...hello
Person().sayHello也是一个函数,可以赋值给变量,并可以直接调用;
>>> Person.sayHello is Person().sayHello
False
>>> Person.sayHello == Person().sayHello
False
Person.sayHello与Person().sayhello并不是同一个对象,直觉上,Person().sayHello关联(绑定)了一个Person实例,而Person.sayHello是一个类方法;
self参数事实上正是方法和函数的区别:方法将它们的第一个参数绑定到所属的实例上,因此这个参数可以不必提供;
>>> class Person:
... name = 'unkown'
... def sayHello(self):
... print('i\'m ' + name)
...
>>>
>>> Person.sayHello(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in sayHello
NameError: name 'name' is not defined
>>> p = Person()
>>> p.name = 'wyj'
>>> p.sayHello()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in sayHello
NameError: name 'name' is not defined
可见,Python在解析变量时,默认从local scope/global scope中查找;
>>> class Person:
... name = 'unkown'
... def sayHello(self):
... print('i\'m ' + self.name)
...
>>>
>>> Person.sayHello(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in sayHello
AttributeError: 'NoneType' object has no attribute 'name'
>>> p = Person()
>>> p.name = 'wyj'
>>> p.sayHello()
i'm wyj
访问成员都要通过self,假如以包含name属性的对象调用Person.sayHello(obj),是否可以呢?
>>> class Cat:
... name = 'huanhuan'
...
>>> Person.sayHello(Cat())
i'm huanhuan
可以,Python并不限制必须用相同类的实例对象作为参数调用类方法(貌似Python的类机制类似Javascript);
访问控制
Python并不直接支持私有方访问,而是要靠程序员自己把握。
不过,可以在属性名称前加上双下划线而给其私有访问能力(对外不可见);
>>> class Person:
... def __private_method(self):
... print('private')
... def test(self):
... self.__private_method()
...
>>> Person().test()
private
>>> Person().__private_method()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Person' object has no attribute '__private_method'
实际上,以上下划线打头的方法都有一个_ClassName__methodName的方法
>>> Person._Person__private_method
<function Person.__private_method at 0x7fed431a2d90>
调用
>>> Person._Person__private_method(None)
private
总之,Python并不能阻止从类外进行方法调用;
类属性以及对象属性
首先,可以为类添加属性,新对象将得到属性的一份拷贝
>>> Person.age = 3
>>> Person().age
3
>>> Person.age = 4
>>> Person().age
4
>>> p = Person()
>>> Person.age = 31
>>> p.age
31
对类属性的修改,反映到了先前生成的对象的属性上,这说明类属性和对象的属性共享一个值;
>>> p.age = 34
>>> p.age
34
>>> Person.age
31
>>> Person.age = 99
>>> p.age
34
而一旦对对象的属性的修改,对象属性就拥有了自己的值,并不会反映到类属性上,而对类属性的修改,也不再反映到该对象的属性上;
这种行为与Javascript类似
类就是一类事物的概括,比如人。
类型是数据类型,有内置的如字符串,数字,复数等,还有其他自定义的,就是自己写的类。
对象和实例是一个东西,是类里具体的东西,如男人,女人,其他人等,这里男人,女人还可以是一类,如再分年老的男人,年轻的男人等。
只要记住:类是一类事物的统称,实例(或对象)是具体的东西。
仅供参考。
举例:
class Person:
'''人的基本属性:名字,年龄,性别等'''
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
class Man(Person):
def __init__(self, name, age):
super(Man, self).__init__(name, age, 'male')
class Woman(Person):
def __init__(self, name, age):
super(Woman, self).__init__(name, age, 'female')
初学的话建议先看
《Learning Python》和《dive into python》,都有中文版的。
www.python.org 是python的官网,上面有很多你想要的资讯,平常可以多上去看看,不过对于e文不是很好的人就有点郁闷。
学一门语言最好是边用边学,所以先到www.python.org那下载并装上python来玩玩。
当你入门了之后,《Python in a Nutshell》是一本挺有用的参考书,这样你就不用记太多东西(比如内置函数或模块),随时翻阅查找。
另外,在你写程序之前先google一下,看看网上有没有写了相似的模块,因为国外经常有一些牛人写了一些很好用的模块,重用它们会提高你的编程速度
相关内容
- python实现去除下载电影和电视剧文件名中的多余字符的
- Python中实现两个字典(dict)合并的方法,pythondict
- python元组操作实例解析,python实例解析
- Python中bisect的用法,Pythonbisect用法
- Python单链表的简单实现方法,python单链实现
- Python显示进度条的方法,python显示进度条
- 跟老齐学Python之list和str比较,齐学pythonstr
- Python实现抓取网页并且解析的实例,python抓取
- Python中捕捉详细异常信息的代码示例,python示例
- Python threading多线程编程实例,threading多线程编程
评论关闭