liveBook · Manning

Persona pattern

As explained in Learn AI-Assisted Python Programming, Second Edition

A persona pattern is a prompting technique that instructs an AI system to respond from a specified role or point of view.

A persona pattern tells an AI system to respond from a chosen role or point of view. Use the template “From now on, act as persona X. Provide outputs that persona X would create.” section 12.1.2 section 12.4

Reviewing code as an instructor

Ask GitHub Copilot to act as an introductory Python instructor who disallows list comprehensions, generators, lambda, and any. When given a password function using any, it identifies the restriction and rewrites the function with a for loop. section 12.1.2

def get_strong_password():
    '''
    Keep asking the user to enter a password until they provide 
    a strong password.
    A strong password has at least one uppercase character, one 
    number, and one punctuation character.
    '''
    # Print the password requirements
    print("Your password must contain at least:")
    print("- One uppercase letter")
    print("- One number")
    print("- One punctuation character")
 
    # Keep asking for a password until a strong one is provided
    while True:
        password = input("Enter a password: ")
 
        # Check if the password meets the requirements
        if not any(c.isupper() for c in password):
            print("Password does not contain an uppercase letter.")
        elif not any(c.isdigit() for c in password):
            print("Password does not contain a number.")
        elif not any(c in string.punctuation for c in password):
            print("Password does not contain a punctuation character.")
        else:
            print("Password is strong!")
     return password

Compare the original password-checking function with the rewritten version, which replaces any-based checks with explicit loop logic. section 12.1.2

Apply the pattern to practical constraints:

  • Ask a manager persona to enforce company naming standards such as camel case. section 12.3
  • Choose the persona pattern when a task needs a defined perspective rather than question-driven requirements gathering. section 12.3 section 12.4

How Learn AI-Assisted Python Programming, Second Edition covers it

How deep it goes
Defines a reusable prompt template and demonstrates it through a constrained Python review, without exploring deeper implementation details.
Examples
Demonstrates an introductory Python instructor, prohibited language features, password validation, and camel-case enforcement.
What it assumes
Assumes a concrete task and teaches the persona prompt structure through an accessible Python example.
What you can do after
Apply it via GitHub Copilot to request perspective-specific reviews, rewrites, and standards checks.
How it connects
Connects prompt patterns with flipped interaction patterns, code review, and coding standards.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn AI-Assisted Python Programming, Second Edition. The section links point to the parts of the book it draws on.