Python writes a class of password generators that require a class variable to count how many passwords are generated altogether. 4 methods Required, 1: Constructor Method 2 Instance Method 3 class Method 4 static method

Source: Internet
Author: User
Tags instance method

Generates a random numeric password of the specified length
Generates a random letter password of the specified length
Generates a mix of random numbers and letters of a specified length

#encoding =utf-8

Import Random

Import string

Class Password_generator:

Password_time=0

def __init__ (self,length):

Self.length=length

def digital_password (self):

Password_generator.password_time+=1

S= ""

For I in Range (self.length):

S+=str (Random.randint (0,9))

return s

@classmethod

def letter_password (cls,length):

Password_generator.password_time+=1

S= ""

For I in range (length):

S+=str (Random.choice (string.letters))

return s

@staticmethod

def letter_mix_digital_password (length):

Password_generator.password_time+=1

S= ""

S1= ""

Nbr=random.randint (0,length)

For I in Range (NBR):

S+=str (Random.choice (string.letters))

For I in Range (LENGTH-NBR):

S1+=str (Random.randint (0,9))

Return S+S1

A=password_generator (10)

Print A.length

Print A.digital_password ()

Print Password_generator.letter_password (10)

Print A.letter_mix_digital_password (10)

Print "Total password:", Password_generator.password_time

C:\python27\scripts>python task_test.py

10

4775220675

Xhqpsybggj

TjDIZwuB19

Total Password:3

Python writes a class of password generators that require a class variable to count how many passwords are generated altogether. 4 methods Required, 1: Constructor Method 2 Instance Method 3 class Method 4 static method

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.