Python object-oriented programming--some class definitions (miscellaneous)

Source: Internet
Author: User

First, Abstractmethod

    • Subclasses must all implement the Abstractmethod method of overriding the parent class
    • Non-Abstractmethod methods can be overridden without implementation
    • A class with a Abstractmethod method cannot be instantiated
  From ABC import Abstractmethod, Abcmeta

1 classBettingstrategy (metaclass=Abcmeta):2 3 @abstractmethod4 defbet (self):5 Print('0')6 7 defRecord_win (self):8 Print('win')9 Ten defRecord_loss (self): One Print('Loss') A - - classBet (bettingstrategy): the defbet (self): - Print('1')

Extension: ABC module

Second, Staticmethod: static function

An object can be called without instantiating a function

1 classHand4:2     def __init__(Self, Dealer_card, *cards):3Self.dealer_card =Dealer_card4Self.cards =Cards5 6 @staticmethod7     defFreeze (Other):8Hand = Hand4 (Other.dealer_card, *other.cards)9         returnHandTen  One @staticmethod A     defSplit (Other, card0, card1): -Hand0 =Hand4 (Other.dealer_card, other.cards[0], card0) -Hand1 = Hand4 (Other.dealer_card, other.cards[1], Card1) the         returnhand0, Hand1 -  -     def __str__(self): -         return ','. Join (Map (str, self.cards))

1 h41 = Hand4 (Deck3.pop (), Deck3.pop (), Deck3.pop ())2     s1, s2 = hand4.split (H41, Deck3.pop (), Deck3.pop ())3     s3 = Hand4.freeze (H41)
called

Python object-oriented programming--some class definitions (miscellaneous)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.