程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python] [skill tree evaluation] skill example - description, improvement and practice [02] - class initialization and initialization parameters

編輯:Python

Preface :

This chapter , Continue to CSDN Some examples of skill tree are improved and practiced . I hope I can help you swim in the skill tree .

Change description

The original example , Too few notes are written , There are too many useless basic concepts to explain , Took time to see , Instead, I was confused . Plus the call choice This random valued function , The result is even more mysterious , This is an example of training copywriting , Suggested amendment .

1 Create a class

# -*- coding: UTF-8 -*-
from random import choice
class Divergence:
def __init__(self,name='robot'):
self.name = name
pass
def getChoice(self,name=None):
if name is not None:
self.name = name
self.__lists = [' scissors ',' stone ',' cloth ']
result = choice(self.__lists)
print(' Class name ',self.name,': Randomly selected >',result)

The above code , Created a Divergence Class ,
among ,

def init(self,name=‘robot’):
self.name = name
pass

Defines the initialization of a class , In addition to here ,self Out of parameters , Also defined ,name Parameters of , At the same time, its default initial value is ‘robot’.

def getChoice(self,name=None):
if name is not None:
self.name = name
self.__lists = [‘ scissors ’,‘ stone ’,‘ cloth ’]
result = choice(self.__lists)
print(‘ Class name ’,self.name,‘: Randomly selected >’,result)

Defines the method of a class , This method starts from the internal list , Take a value at random and output , That is to say, it realizes the function of guessing fist .

1.1 Python Create class with initialization parameters

The above example , We manually added the class initialization constructor ,init.
and , stay init except ,self Out of parameters , There are other parameters name, This is a creation with initialization .

1.2 Python Create class without initialization parameters

If , Not at all init Construction method of class , perhaps , The construction method is given , however , The only initialization parameters entered are self, This is the creation class without initialization parameters .
If you don't give a class constructor ,Python It will give its own default construction method , This has been mentioned in the previous discussion .

2 The use of the class

2.1 Initialization class with parameters

# When instantiating an object, pass name Parameters 
d = Divergence('A')
d.getChoice()

【 When instantiating this example , Parameters entered ’A’, When initializing, change the name of the class to A 了 ,】

Class name A : Randomly selected > cloth

【 Change input parameters 】

# When instantiating an object, pass name Parameters 
d = Divergence(' Who am I ')
d.getChoice()

Class name Who am I : Randomly selected > scissors

【 case , Never mind “ Randomly selected >” Result , As long as there is output , Because design is a random number 】

【 Now? , We don't enter parameters for him 】

# When instantiating an object, pass name Parameters 
d = Divergence()
d.getChoice()

Class name robot : Randomly selected > scissors

Now , The class name becomes the default name we set at the beginning :robot

2.2 Initializing classes without parameters

Now we are rewriting the class initialization definition , This time, , We give the class initialization definition , however , We pass 了 , There is nothing specific in it , Let's see the result :

# -*- coding: UTF-8 -*-
from random import choice
class Divergence:
def __init__(self):
pass
def getChoice(self,name=None):
self.__lists = [' scissors ',' stone ',' cloth ']
result = choice(self.__lists)
print(' Class name ',self,': Randomly selected >',result)
# When instantiating an object, pass name Parameters 
d = Divergence()
d.getChoice()

Class name <main.Divergence object at 0x000002087EED2BC8> : Randomly selected > cloth

OK, Now we see that the compiler has given him a good name :
main.Divergence object at 0x000002087EED2BC8>

A little more violence , This time the initialization is not written at all , Don't write at all

# -*- coding: UTF-8 -*-
from random import choice
class Divergence:
def getChoice(self,name=None):
self.__lists = [' scissors ',' stone ',' cloth ']
result = choice(self.__lists)
print(' Class name ',self,': Randomly selected >',result)
# When instantiating an object, pass name Parameters 
d = Divergence()
d.getChoice()

Output :

Class name <main.Divergence object at 0x000002087EEDA548> : Randomly selected > scissors

It seems that the code pointer has changed .

Summary :

I believe that writing here , The discussion about the initialization parameters of the class is clear .

Reference resources

choice() function

From the list given , Random items in tuples or strings , Random Return an item :

#!/usr/bin/python
import random
print "choice([1, 2, 3, 5, 9]) : ", random.choice([1, 2, 3, 5, 9])
print "choice('A String') : ", random.choice('A String')

choice([1, 2, 3, 5, 9]) : 5
choice(‘A String’) : S


case , here ,choice Function from [1, 2, 3, 5, 9] In the list , Got a number at random 5,
From a string ,‘A String’, A character is randomly selected from the S


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved