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 .
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 .
# -*- 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 .
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 .
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 .
# 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
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 .
I believe that writing here , The discussion about the initialization parameters of the class is clear .
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
】