What does self and / mean in python 3.7? [duplicate] - python

This question already has answers here:
What is the meaning of a forward slash "/" in a Python method signature, as shown by help(foo)? [duplicate]
(1 answer)
What does the slash mean when help() is listing method signatures?
(3 answers)
What is the purpose of the `self` parameter? Why is it needed?
(26 answers)
Closed 4 years ago.
When using help function on some method, I get a short text describing that method. In the first line, most of the time it says:
"method name" (self, iterable, /).
Can anyone tell me what does self and / mean for that method?

Related

Why does an outer function call an inner function in python, without explicitly calling it? [duplicate]

This question already has answers here:
Python decorator? - can someone please explain this? [duplicate]
(7 answers)
How do I make function decorators and chain them together?
(20 answers)
Closed 3 months ago.
This post was edited and submitted for review 3 months ago and failed to reopen the post:
Original close reason(s) were not resolved
NOTE: This question is not directly related to decorators, but rather the concept of them.
It is NOT related to these 2 questions:
What is decorator with "#" above a function in Python? [duplicate] (7 answers)
How do I make function decorators and chain them together? (19 answers)
MY QUESTION:
I'm currently experimenting with inner functions to get a good grasp of python decorators.
With this code below, I am able to successfully print "sup" through the wrapper function, even though I didnt explicitly call it.
def my_decorator(test):
def wrapper():
print(test)
return wrapper
hello = my_decorator("sup")
hello()
This however, doesnt work if I do any of the following
remove return wrapper
directly call my_decorator("sup") without passing it into the hello function
correspondingly, I have 2 questions:
does return wrapper automatically call the wrapper function?
What's the difference between using a reference function like hello, and directly calling the my_decorator function?

What does singular "*" as an argument in a python function definition do? [duplicate]

This question already has an answer here:
Keyword only parameter [duplicate]
(1 answer)
Closed 3 months ago.
I am trying to look through some code and don't know what the asterisk in the following code means.
def pylog(func=None, *, mode='cgen', path=WORKSPACE, backend='vhls', \
board='ultra96', freq=None):
What does the lonely asterisk signify in a function definition when not followed by the name of an argument?
I can only find results for *foo.
This syntax forces arguments after the * to be called with their keyword names when someone calls the function/method.
Example:
# This is allowed
pylog(math.log, mode='cgen')
# This is *NOT* allowed
pylog(math.log, 'cgen')

python What does `*` and `/` mean in def ()? [duplicate]

This question already has answers here:
Positional argument vs keyword argument
(9 answers)
Closed 6 months ago.
I found * , / in the typing source code, but I don't understand what it means.
def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
pass
Anything before / means that the argument is position-only (see PEP-570, while anything after *is keyword only (see PEP-3102). Anything in between can be both positional and keyword.

Express a method like a keyword [duplicate]

This question already has answers here:
Can you add new statements to Python's syntax?
(13 answers)
Closed 4 years ago.
How can I express a method (e.g. dir(os)) like a keyword (e.g. dir_ os)? Is it even possible?
The opposite is quite easy to achieve:
# `assert` expressed like a method
def assert_(x): assert x
assert_(1 == 1)
No, it's not possible. Parentheses are required in order to call a function or method.

How can i convert a string in a python method? [duplicate]

This question already has answers here:
Calling a function of a module by using its name (a string)
(18 answers)
Closed 8 years ago.
I receive a string, for example "save". And i have a method save with paramethers.
How can i convert the string save in a call to save().
I tried with eval and exec.
Edit: Solved here --> Calling a function of a module from a string with the function's name in Python
def some_method(self):
save_method = getattr(self, 'save')
save_method() # save()

Categories