Init method used in Python and the random number function

1, using init__ __ method
2, using the method of random

class Person(object):
 '''
 这里定义的属性 为 静态的
 '''
 empCount = 0

 # 创建对象的时候自动执行
 def __init__(self, name):
  print('初始化方法。。。。。')
  self.name = name

 def eat(self):
  print(self.name, "eat 方法。。。。")


p = Person('张三')
p.eat()

p1 = Person('李四')
p1.eat()

operation result:

Initialization method. . . . .
Joe Smith eat method. . . .
Initialization method. . . . .
John Doe eat method. . . .

Init method used in Python and the random number function

empCount variable is a variable, its value will be shared between all instances of this class. You can access internal use Employee.empCount in class or outside class.

 A first method __init __ () method is a special method, constructor, or class initialization method is called when an instance of the class the method is called created. The method members all object instances.

Examples of self on behalf of the class, self-defined class when the method must be some, although not necessarily the appropriate parameters passed when calling.

Random number function

Invoked by the module name. Method, you need to import the module random.

# 第一步导入模块
import random

# 使用 模块名称.方法
# randint 产生 整数类型的数据,包含头和尾
num = random.randint(0, 3)
print(num)

Init method used in Python and the random number function

The above is a python in the init method and random method small series to introduce Detailed integration, we want to help, if you have any questions please give me a message.

Guess you like

Origin www.linuxidc.com/Linux/2019-06/158928.htm