Skip to content

python class

从接触python开始大部分都是基于Django进行开发,做的相对于简单得多。而python一切都是面向对象,今天需要做一个功能模块的封装,中间遇到关于继承、私有变量(方法)记录下一些开发中遇到的问题。

1、2.2版本以后使用new style class,任何类都必须继承某个类,如果不需要继承某个类也要继承object。当然如果你还是用老办法来处理子类对父类初始化的话,那就不需要关心这个。


class People(object):
 # 姓名
 name = None
 def __init__(self, name = None):
 self.name = name

 class Student(People):
 def __init__(self, name = None):
 #如果使用super对父类声明的话,哪怕父类不需要继承对象的话,那必须继承object对象
 #老办法不再说明
 super(Student, self).__init__(name)


2、私用变量,具体用法看Python Document,需要注意的是不管是公共变量(方法)、私用变量(方法),在类内部使用他们都加上self.,也许这个习惯可以给你带来开发‘乐趣’。

相关日志

Categories: 开源, 技术.

Tags:

Comment Feed

One Response

  1. 啊哈哈~我还是看不懂,只能留下一个小小的足迹,呵呵~~~~~



Some HTML is OK

or, reply to this post via trackback.

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word