第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
  • class Person(object):

    ? ?__slots__ = ('name', 'gender')

    ? ?def __init__(self, name, gender):
    ? ? ? ?self.name = name
    ? ? ? ?self.gender = gender


    class Student(Person):

    ? ?__slots__ = ('score',)

    ? ?def __init__(self, name, gender, score):
    ? ? ? ?self.name = name
    ? ? ? ?self.gender = gender
    ? ? ? ?self.score = score


    s = Student('Bob', 'male', 59)
    s.name = 'Tim'
    s.score = 99
    print(s.score)

    注:__slots__ = ('name', 'gender') 限定屬性,不能動態(tài)添加屬性

    查看全部
  • 通過type()函數,可以獲得變量的類型。

    通過dir()方法,可以獲取變量的所有屬性:在dir列出的屬性中,有很多是以下劃線開頭和結尾的,這些都是特殊的方法,稱為內建方法,如果已知一個屬性名稱,要獲取或者設置對象的屬性,就需要用 getattr() 和 setattr( )函數了。

    >>> getattr(p, 'name') # 獲取name屬性

    'Alice'

    >>> setattr(p, 'name', 'Adam') # 設置新的name屬性

    >>> s.name

    'Adam'

    查看全部
  • 函數isinstance()可以判斷一個變量的類型。

    >>> isinstance(s, Person)
    True # s是Person類型
    >>> isinstance(s, Student)
    True # s是Student類型
    >>> isinstance(s, Teacher)
    False # s不是Teacher類型

    查看全部
    0 采集 收起 來源:Python判斷類型

    2025-05-12

  • 對人類的抽象可以定義為Person類,而學生、老師等,也都是人類,所以,在Python當中,如果定義學生Student的類,可以繼承Person類。

    接著定義Student類,在定義Student類的時候,由于繼承了Person類,所以Student類自動擁有name、gender屬性,因此,在定義Student類的時候,只需要把額外的屬性加上即可。

    class Student(Person):

    ? ? def __init__(self, name, gender, score):

    ? ? ? ? super(Student, self).__init__(name, gender)

    ? ? ? ? self.score = score


    student = Student('Alice', 'girl', 100)

    print(student.name) # ==> Alice

    print(student.gender) # ==> girl

    print(student.score) # ==> 100

    在定義繼承類的時候,有幾點是需要注意的:

    1. class Student()定義的時候,需要在括號內寫明繼承的類Person

    2. 在__init__()方法,需要調用super(Student, self).__init__(name, gender),來初始化從父類繼承過來的屬性

    查看全部
    0 采集 收起 來源:Python繼承類

    2025-05-12

  • 為了操作實例對象的私有屬性,我們定義了實例方法;同樣的,如果需要需要操作類的私有屬性,則應該定義類的方法。

    默認的,在class中定義的全部是實例方法,實例方法第一個參數 self 是實例本身。

    要在class中定義類方法,需要這么寫:

    class Animal(object):

    ? ? __localtion = 'Asia'

    ? ? def __init__(self, name, age):

    ? ? ? ? self.name = name

    ? ? ? ? self.age = age


    ? ? @classmethod

    ? ? def set_localtion(cls, localtion):

    ? ? ? ? cls.__localtion = localtion


    ? ? @classmethod

    ? ? def get_localtion(cls):

    ? ? ? ? return cls.__localtion


    print(Animal.get_localtion()) # ==> Asia

    Animal.set_localtion('Afica')

    print(Animal.get_localtion()) # ==> Africa

    和實例方法不同的是,這里有兩點需要特別注意:

    類方法需要使用@classmethod來標記為類方法,否則定義的還是實例方法

    類方法的第一個參數將傳入類本身,通常將參數名命名為 cls,上面的 cls.__localtion 實際上相當于Animal.__localtion。

    ? ??? ?因為是在類上調用,而非實例上調用,因此類方法無法獲得任何實例變量,只能獲得類的引用。

    總結:

    實例方法def 方法(self,); 類方法def 方法(cls),且前面須加上@classmethod。

    類方法只能訪問類變量引用,不能訪問實例內的變量。

    查看全部
  • 把Animal類的age、name、localtion定義成私有屬性,并定義對應的方法修改和獲取他們的值。

    class Animal(object):
    ? ?def __init__(self, name, age, localtion):
    ? ? ? ?self.__name = name
    ? ? ? ?self.__age = age
    ? ? ? ?self.__localtion = localtion

    ? ?def set_name(self, name):
    ? ? ? ?self.__name = name

    ? ?def get_name(self):
    ? ? ? ?return self.__name

    ? ?def set_age(self, age):
    ? ? ? ?self.__age = age

    ? ?def get_age(self):
    ? ? ? ?return self.__age

    ? ?def set_localtion(self, localtion):
    ? ? ? ?self.__localtion =localtion

    ? ?def get_localtion(self):
    ? ? ? ?return self.__localtion

    p=Animal("ww","12","we")

    print(p.get_name(),p.get_age(),p.get_localtion())

    注:本例中如果去除def set_name(self, name):等,程序也能正常運行,待解。


    ?def get_info(self):

    ? ? ? ? return 'name = {}, age = {}, localtion = {}'.format(self.name, self.age, self.localtion)

    這個語句可以同時取得多個屬性

    查看全部
  • 并不是所有的屬性都可以被外部訪問的,這種不能被外部訪問的屬性稱為私有屬性。私有屬性是以雙下劃線'__'開頭的屬性。

    # 類私有屬性

    class Animal(object):

    ? ? __localtion = 'Asia'


    print(Animal.__localtion)


    Traceback (most recent call last):

    ? File "<stdin>", line 1, in <module>

    AttributeError: type object 'Animal' has no attribute '__localtion'

    # 實例私有屬性
    class Animal(object):
    ? ?def __init__(self, name, age, localtion):
    ? ? ? ?self.name = name
    ? ? ? ?self.age = age
    ? ? ? ?self.__localtion = localtion

    dog = Animal('wangwang', 1, 'GuangDong')
    print(dog.name) # ==> wangwang
    print(dog.age) # ==> 1
    print(dog.__localtion)

    Traceback (most recent call last):
    ?File "<stdin>", line 1, in <module>
    AttributeError: 'Animal' object has no attribute '__localtion'

    查看全部
  • # Enter a code

    class Animal(object):

    ? ? localtion = 'Asia'

    ? ? def __init__(self, name, age, localtion):

    ? ? ? ? self.name = name

    ? ? ? ? self.age = age

    ? ? ? ? self.localtion = localtion

    dog = Animal('wangwang', 1, 'GuangDong')

    print(dog.localtion)

    print(Animal.localtion)

    以上localtion為公共屬性


    class Animal(object):

    ? ? __count = 0

    ? ? def __init__(self, name):

    ? ? ? ? Animal.__count = Animal.__count + 1

    ? ? ? ? self.name = name

    ? ? ? ? print(Animal.__count)


    p1 = Animal('Cat')

    p2 = Animal('Dog')


    print(Animal.__count)

    以上__count為私有屬性,實例不能訪問

    查看全部
  • class Animal(object):

    ? ? count = 0

    ? ? def __init__(self, name, age):

    ? ? ? ? self.name = name

    ? ? ? ? self.age = age

    ? ? ? ? Animal.count += 1


    dog = Animal('wangwang', 1)

    print(Animal.count)

    cat = Animal('mimi', 3)

    print(Animal.count)

    pig = Animal('panpan', 1)

    print(Animal.count)

    請給 Animal類添加一個類屬性 count,每創(chuàng)建一個實例,count 屬性就加 1,這樣就可以統(tǒng)計出一共創(chuàng)建了多少個 Animal的實例。

    查看全部
    0 采集 收起 來源:Python類屬性

    2025-05-11

  • 在Python中,通過class關鍵字定義一個類,比如我們需要定義一個人的類。按照 Python 的編程習慣,類名以大寫字母開頭。因此可以這樣定義:

    class Person: ?pass

    注意,在這個Person類的定義里面,并沒有繼承任何類,除了這樣定義以外,還可以有以下兩種定義方式。

    class Person(): pass ?class Person(object): ?pass

    這三種情況有什么區(qū)別呢?在Python3中,是沒有區(qū)別的,但是在Python2中,則有一定的區(qū)別。
    在Python2中,對于第一種定義的方法,Person類只有有限的幾個內建函數'__doc__', '__module__', 'name',而對于第二種、第三種定義的方法,則會繼承Python object對象的更多的內建函數,可以更便捷的操作對象。這是Python2版本的差異。在Python3中,我們只需要知道這三種方式都可以定義一個類即可。

    查看全部
  • 請定義一個動物類,抽象出名字、年齡兩個屬性,并實例化兩個實例dog, cat。

    class Animal(object):
    ? ?def __init__(self, name, age):
    ? ? ? ?self.name = name
    ? ? ? ?self.age = age

    dog = Animal('wangwang', 1)
    cat = Animal('mimi', 3)
    print(dog.name)
    print(dog.age)
    print(cat.name)
    print(cat.age)

    需要注意的是,__init__() 方法的第一個參數必須是 self(也可以用別的名字,但建議使用習慣用法),后續(xù)參數則可以自由指定,和定義函數沒有任何區(qū)別。
    定義類后,就可以相應的實例化對象了,需要注意的是,在實例化的時候,需要提供除self以外的所有參數。

    查看全部
  • 斐波那契數列

    查看全部
  • 數學函數

    查看全部
    0 采集 收起 來源:Python導入模塊

    2025-03-28

  • 有必要注意的是,返回函數和返回函數值的語句是非常類似的,返回函數時,不能帶小括號,而返回函數值時,則需要帶上小括號以調用函數。

    # 返回函數
    def myabs():
    ? ?return abs

    # 返回函數值
    def myabs(x):
    ? ?return abs(x)

    查看全部
    0 采集 收起 來源:Python返回函數

    2025-03-07

  • math 就相當于一個工具箱,import math,相當于把工具箱搬到家中了,但是你工具箱不打開是用不了工具的?;蛘哒f家里還有別的工具箱,你要用工具的話,必須先找到對工具箱math,然后再用工具sin,所以用 math.sin().

    sin()相當于一件具體的工具,from math import sin,相當于你直接從工具箱中拿出了工具,所以可以直接用。如:sin(100)

    而 from math import * ,相當于把工具箱工具一股腦倒在了地上,你可以直接用,想用哪一個就用哪一個。

    查看全部
    0 采集 收起 來源:Python導入模塊

    2025-03-02

  • a=0

    b=1

    注意

    a=b

    b=a+b? ? ? ? ? ? ? ? 與 a,b = b,a+b 兩個句式的區(qū)別。

    查看全部
  • Python判斷類型

    隨著我們學習步伐的前進,我們的程序會出現越來越多的類型,有我們自己定義的類,也有Python自有的str、list、dict等,他們的本質都是都是Python中的一種數據類型,這時有必要去判斷數據的類型,通過函數isinstance()可以判斷一個變量的類型。

    class Person(object): ? ? def __init__(self, name, gender): ? ? ? ? self.name = name ? ? ? ? self.gender = gender class Student(Person): ? ? def __init__(self, name, gender, score): ? ? ? ? super(Student, self).__init__(name, gender) ? ? ? ? self.score = score class Teacher(Person): ? ? def __init__(self, name, gender, course): ? ? ? ? super(Teacher, self).__init__(name, gender) ? ? ? ? self.course = course p = Person('Tim', 'Male') s = Student('Bob', 'Male', 88) t = Teacher('Alice', 'Female', 'English')

    當我們拿到變量 p、s、t 時,可以使用 isinstance 判斷類型:

    >>> isinstance(p, Person) True # p是Person類型 >>> isinstance(p, Student) False # p不是Student類型 >>> isinstance(p, Teacher) False # p不是Teacher類型

    這說明在繼承鏈上,一個父類的實例不能是子類類型,因為子類比父類多了一些屬性和方法。
    我們再考察 s:

    >>> isinstance(s, Person) True # s是Person類型 >>> isinstance(s, Student) True # s是Student類型 >>> isinstance(s, Teacher) False # s不是Teacher類型

    s 是Student類型,不是Teacher類型,這很容易理解。但是,s 也是Person類型,因為Student繼承自Person,雖然它比Person多了一些屬性和方法,但是,把 s 看成Person的實例也是可以的。

    這說明在一條繼承鏈上,一個實例可以看成它本身的類型,也可以看成它父類的類型。

    isinstance也可以用于Python自有數據類型的判斷。

    s = 'this is a string.' n = 10 isinstance(s, int) # ==> False isinstance(n, str) # ==> False

    查看全部
    0 采集 收起 來源:Python判斷類型

    2025-03-01

首頁上一頁1234567下一頁尾頁

舉報

0/150
提交
取消
課程須知
本課程是Python入門的后續(xù)課程 1、掌握Python編程的基礎知識 2、掌握Python函數的編寫 3、對面向對象編程有所了解更佳
老師告訴你能學到什么?
1、什么是函數式編程 2、Python的函數式編程特點 3、Python的模塊 4、Python面向對象編程 5、Python強大的定制類

微信掃碼,參與3人拼團

微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

友情提示:

您好,此課程屬于遷移課程,您已購買該課程,無需重復購買,感謝您對慕課網的支持!