python?????????????????????

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python?????????????????????相关的知识,希望对你有一定的参考价值。

?????????attr   htm   com   ror   ??????   ??????   ???????????????   ??????   attribute   

0911--https://www.cnblogs.com/fnng/archive/2013/05/08/3066054.html

?????????????????????

 

????????????

????????????????????????????????????_metaclass = type???????????????????????????

class NewType(Object):
    more_code_here
class OldType:
    more_code_here

??????????????????NewType????????????OldType????????????????????????????????????

 

????????????

?????????????????????????????????????????????????????????????????????????????????????????????????????????Python????????????????????????????????????init?????????????????????init??????????????????????????????__init__?????????????????????

class FooBar:
    def __init__(self):
        self.somevar = 42

#????????????
>>> f = FooBar()
>>> f.somevar
42

 

????????????????????????

?????????????????????????????????????????????????????????????????????????????????????????????????????????

???Python pass?????????????????????????????????????????????????????????pass ???????????????????????????????????????????????????

??????B?????????hello?????????B????????????A?????????????????????A??????hello?????????

class A:
    def hello(self):
        print (???hello,I am A.???)
class B(A):
    pass

#????????????
>>> b = B()
>>> b.hello()
hello,I am A.

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

class A:
    def hello(self):
        print (???hello,I am A.???)
class B(A):
    def hello(self):
        print (???hello,I am B???)

#????????????
>>> b = B()
>>> b.hello()
hello,I am B

 

?????????????????????

??????????????????????????????????????????????????????????????????????????????????????????????????????

class Bird:
    def __init__(self):
        self.hungry = True
    def eat(self):
        if self.hungry:
            print (???Aaaah...???)
            self.hungry = False
        else:
            print (???No,thanks!???)

#????????????
>>> b = Bird()
>>> b.eat()
Aaaah...
>>> b.eat()
No,thanks!

??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

?????????SongBird????????????Bird??????????????????????????????????????????

class Bird:
    def __init__(self):
        self.hungry = True
    def eat(self):
        if self.hungry:
            print (???Aaaah...???)
            self.hungry = False
        else:
            print (???No,thanks!???)

class SongBird(Bird):
    def __init__(self):
        self.sound = ???qiuqiu~~???
    def sing(self): 
        print (self.sound)

#????????????
>>> s = SongBird()
>>> s.sing()
qiuqiu~~
>>> s.eat()
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    s.eat()
  File "E:/study/python/demo0911/??????????????????.py", line 5, in eat
    if self.hungry:
AttributeError: ???SongBird??? object has no attribute ???hungry???

????????????????????????????????????SongBird??????hungry?????????????????????????????????SongBird??????????????????????????????????????????????????????????????????????????????hungry????????????????????????????????????????????????SongBird????????????????????????????????????Bird???????????????????????????????????????????????????

?????????????????????

??????????????????????????????????????????

class Bird:
    def __init__(self):
        self.hungry = True
    def eat(self):
        if self.hungry:
            print (???Aaaah...???)
            self.hungry = False
        else:
            print (???No,thanks!???)

class SongBird(Bird):
    def __init__(self):
        Bird.__init__(self)
        self.sound = ???qiuqiu~~???
    def sing(self):
        print (self.sound)

#????????????
>>> s = SongBird()
>>> s.sing()
qiuqiu~~
>>> s.eat()
Aaaah...
>>> s.eat()
No,thanks!

???SongBird???????????????????????????Bird.__init__(self)???????????????????????????????????????????????????self?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????self???????????????????????????????????????????????????

??????????????????????????????self?????????????????????????????????SongBird????????????????????????????????????????????????????????????????????????

 

????????????super??????

__metaclass__ = type    #??????????????????
class Bird:
    def __init__(self):
        self.hungry = True
    def eat(self):
        if self.hungry:
            print (???Aaaah...???)
            self.hungry = False
        else:
            print (???No,thanks!???)

class SongBird(Bird):
    def __init__(self):
        super(SongBird,self).__init__()
        self.sound = ???qiuqiu~~???
    def sing(self):
        print (self.sound)

#????????????
>>> s = SongBird() >>> s.sing() qiuqiu~~ >>> s.eat() Aaaah... >>> s.eat() No,thanks!

super??????????????????????????????????????????????????????????????????super?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????SongBird????????????????????????Bird??????????????????super(SongBird,self)???

 

??????

???????????????????????????????????????????????????getHeight???setHeight?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

class Rectangle:
    def __init__(self):
        self.width = 0
        self.height = 0
    def setSize(self,size):
        self.width,self.height = size
    def getSize(self):
        return self.width,self.height

#????????????
>>> r = Rectangle()
>>> r.width = 10
>>> r.height = 5
>>> r.getSize()
(10, 5)
>>> r.setSize((150,100))
>>> r.width
150

????????????????????????getSize???setSize?????????????????????size????????????????????????????????????size??????width???height??????????????????

 

property??????

property??????????????????????????????????????????????????????????????????Rectangle ????????????????????????????????????????????????

__metaclass__ = type
class Rectangle:
    def __init__(self):
        self.width = 0
        self.height = 0
    def setSize(self,size):
        self.width,self.height = size
    def getSize(self):
        return self.width,self.height
    size = property(getSize,setSize)

#????????????
>>> r = Rectangle()
>>> r.width = 10
>>> r.height = 5
>>> r.size
(10, 5)
>>> r.size = 110,120
>>> r.width
110

??????????????????Retangle ??????property ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????size ??????????????????????????????????????????????????????????????????????????????????????????width???height ???size???

 


以上是关于python?????????????????????的主要内容,如果未能解决你的问题,请参考以下文章

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python

python初识