How to leave a comment of a function folded in the piecham on the line where the function is defined

Asked 2 years ago, Updated 2 years ago, 102 views

Press Ctrl+Alt+T to set the folding settings for a specific section in the pie chamber. It looks like this:

# pragma region This is the folding section
Blah blah
# # end region

This folds as shown below

[Gray] This is the folding section...

I like it because I can comment on the folding line right away.

Without setting this folding interval, most functions automatically support folding from the definition to the end of the function.

def fold_it():
    # This function is a function that shows folding.
    Blah blah
    Blah blah

The folds as shown below.

def fold_it():...

I'm good at leaving comments on most functions, but if I fold it like this, it's hard to see what this function does, so I want it to be like this.

deffold_it():... # This is a function that shows folding.

You can write an annotation separately instead of an annotation left inside,

Is there a way to leave a comment on a function on the same line as the function's definition if you fold it in the piecham?

python pycharm

2022-09-21 18:06

1 Answers

Folding is supported by PyCham, and pragma is not the basic of python.

PEP stipulates that the annotations of functions are written in the form of docstring. Write docstring as follows

ef discount_rewards(rewards, gamma=FLAGS.gamma):
    """Returns discounted rewards by a rate, `gamma`

    When a reward is nonzero,
    the game has been reset ("Pong" specific)

    Parameters
    ----------
    rewards : 1-D Array
        Rewards np.ndarray of shape (n_samples,)
    gamma : float, optional
        Discount rate. Usually it should be handled
        by `FLAGS.gamma` (default: 0.99)

    Returns
    -------
    dicounted_r : 1-D Array
        Discounted rewards of shape (n_samples,)

    Example
    -------
    >>> r = np.array([1, 1, 1])
    >>> discount_rewards(r, 0.99)
    np.array([1 + 0.99 + 0.99**2, 1 + 0.99, 1])
    """
    discounted_r = np.zeros_like(rewards)
    return discounted_r

If you write a docstring in the form above, the docstring will be exposed to the screen when using the function in IDE, etc.

I didn't use PiCham, so I couldn't test how the folding would turn out.


2022-09-21 18:06

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.