Built-in class methods

# Inextricably built-in class methods and built-in functions Information 
# bis under Method 
# obj .__ str__ STR (obj) 
# obj .__ repr__ the repr (obj) 
# class Teacher: 
#      DEF the __init __ (Self, name, the salary): 
#          the self.name name = 
#          self.salary the salary = 
#      DEF __str __ (Self): 
#          return "Teacher apos Object:% S"% the self.name 
#      DEF __repr __ (Self): 
#          return STR (Self .__ dict__ magic) 
#      DEF FUNC (Self): 
#          return 'Wahaha' 
# Nezha = Teacher ( 'Zha', 250) 
#print (nezha) # print an object when the call is str__ .__ A 
# Print (repr (Nezha)) 
# Print ( '>>>% r'% Nezha) 
# A .__ str__ -> Object 
# Object there is a __str__, once invoked, it calls this method returns the object memory address 
# L = [1,2,3,4,5] # instantiates an instance of an object class list 
# Print (L) 
# % S str () direct printing actually go __str__ 
# % r repr () actually go __repr__ 
# repr str is the spare tire, but can not do repr str spare tire 

# Print (obj) / ' % s'% obj / str ( obj) when, in fact, internal calls obj .__ str__ method, str method if there is, then he must return a string 
# If you do not __str__ method, will go first __repr__ method of the present class, find no parent class __str__. 
# Repr (), only to find __repr__, if not find the parent class 

# method has many built- 
# necessarily all in the object
# Class the Classes: 
#      DEF the __init __ (Self, name): 
#          the self.name name = 
#          self.student = [] 
#      DEF __len __ (Self): 
#          return len (self.student) 
#      DEF __str __ (Self): 
#          return 'classes' 
# py_s9 the classes = ( 'Python full stack 9') 
# py_s9.student.append ( 'brother') 
# py_s9.student.append ( 'Taige') 
# Print (len (py_s9)) 
# Print (py_s9) 

# the __del__ 
# class a: 
#      DEF __del __ (Self): # destructor: some finishing work before deleting an object 
#         self.f.close () 
# A = A () 
# AF = Open () # open a document file to get a first open file operator exists in the operating system memory 
# del A file operation took AF # disappeared in the character memory 
# del del # a performs both this method, and delete variable 
# reference count 


# the __call__ 
class a:
     DEF  the __init__ (Self, name): 
        the self.name = name
     DEF  the __call__ (Self):
         '' ' 
        All of the properties of the print object 
        : return: 
        ' '' 
        for K in Self. the __dict__ :
             Print (K, Self. the __dict__[k])
a = A('alex')()
= {DIC ' K ' : ' V ' }
 # Object: storage properties and call methods 
DIC [ ' K ' ] = ' V ' 
# class Foo: 
#      DEF the __init __ (Self, name, Age, Sex): 
#          the self.name name = 
#          self.age Age = 
#          self.sex Sex = 
# 
#      DEF the __getitem __ (Self, Item): 
#          IF the hasattr (Self, Item): 
#              return Self .__ dict __ [Item] 
# 
#      DEF __setitem __ (Self, Key, value): 
#         Self .__ dict __ [Key] = value 
# 
#      DEF __delitem __ (Self, Key): 
#          del Self .__ dict __ [Key] 
# 
# F = Foo ( 'Egon', 38 is, 'M') 
# Print (F [ 'name' ]) 
# F [ 'Hobby'] = 'M' 
# Print (f.hobby, F [ 'Hobby']) 
# del f.hobby # Object native support __delattr__ 
# del F [ 'Hobby'] # achieved through their the 
# Print (f .__ dict__) 

# __init__ initialization method 
# __new__ constructor: create an object 
class a:
     DEF  __init__ (Self): 
        Self.x = 1
        print('in the init function ' )
     # DEF __new __ (CLS, * args, ** kwargs): 
    #      Print (' in new new function ') 
    #      return Object .__ new new __ (A, * args, ** kwargs) 

# A1 = A () 
# A2 = a () 
# A3 = a () 
# Print (A1) 
# Print (A2) 
# Print (A3) 
# Print (AX) 

# design pattern 
# 23 kinds of 
# singleton pattern 
# a class is always only one instance 
# when the first time you instantiate this class when you create an instance of the object 
# when you come back after instantiation previously created on object- 

# class a: 
#      __instance = False
#     def __init__(self,name,age):
#         self.name = name
#         self.age = age
#     def __new__(cls, *args, **kwargs):
#         if cls.__instance:
#             return cls.__instance
#         cls.__instance = object.__new__(cls)
#         return cls.__instance
#
# egon = A('egg',38)
# egon.cloth = '小花袄'
# nezha = A('nazha',25)
# print(nezha)
# print(egon)
# print(nezha.name)
# print(egon.name)
# print(nezha.cloth)


# class A:
#     def __init__(self,name):
#         self.name = name
#
#     def __eq__(self, other):
#         if self.__dict__ == other.__dict__:
#             return True
#         else:
#             return False
#
# ob1 = A('egon')
# ob2 = A('egg')
# print(ob1 == ob2)

# hash()   #__hash__
# class A:
#     def __init__(self,name,sex):
#         self.name = name
#         self.sex = sex
#     def __hash__(self):
#         return hash(self.name+self.sex)
#
# a = A('egon','男')
# b = A('egon','nv')
# print(hash(a))
# print(hash(b))
import json
from collections import namedtuple
Card = namedtuple('Card',['rank','suit'])   # rank 牌面的大小 suit牌面的花色
# class FranchDeck:
#     ranks = [str(n) for n in range(2,11)] + list('JQKA')   # 2-A
#     suits = ['红心','方板','梅花','黑桃']
#
#     def __init__(self):
#         self._cards = [Card(rank,suit) for rank in FranchDeck.ranks
#                                         for suit in FranchDeck.suits]
#
#     def __len__(self):
#         return len(self._cards)
#
#     def __getitem__(self, item):
#         return self._cards[item]
#
#     def __setitem__(self, key, value):
#         self._cards[key] = value
#
#     def __str__(self):
#         json.dumps return (self._cards, ensure_ascii = False) 

# Deck FranchDeck = () 
# Print (Deck [10]) 
# from Random Import Choice 
# Print (Choice (Deck)) 
# # Print (Choice (Deck)) 
# Random shuffle Import from 
# shuffle (Deck) 
# Print (Deck [10]) 
# Print (Deck) 
# : Print (Deck [. 5]) 

# built-in built-in built-in functions method of base-type built-in module <---> class 
# == __eq__ 
# len () __len__ 

# 100 names and ages of different sex 
# the SET 
# class A: 
#      DEF __init __ (Self, name, sex, Age):
#         self.name = name
#         self.sex = sex
#         self.age = age
#
#     # def __eq__(self, other):
#     #     if self.name == other.name and self.sex == other.sex:
#     #         return True
#     #     return False
#
#     def __hash__(self):
#         return hash(self.name + self.sex)
# a = A('egg','男',38)
# b = A('egg','男',37)
# print(set((a,b)))   # unhashable

# set 依赖对象的 hash eq

Guess you like

Origin www.cnblogs.com/qwer-123/p/11311185.html