class definition in python

class define in python:

class AddressBookEntry(object):
    version = 0.1
    def __init__(self, name, phone):
        self.name = name
        self.phone = phone
    def update_phone(self, phone):
        self.phone = phone

subclass of the class:

class EmployeeAddressBookEntry(AddressBookEntry):
    def __init__(self, name, phone, id, social):
        AddressBookEntry.__init__(self, name, phone)
        self.empid = id
        self.ssn = social

The object in the brackets is the class which we inherit from. 

 

dynamic instance attributes, those that are not declared anywhere in the class definition, yet can be created “on the fly.”
We can use this like this:

Bob = AddressBookEntry('Bob', '2b7474748')

Bob.JJ = None

This inner class is a real Python class, but is only visible to instances of the MyClass class.
class MyClass(object):
    class InnerClass:
        pass

 

posted on 2012-09-08 12:34  很遗憾我不是  阅读(426)  评论(0编辑  收藏  举报