This question already has answers here:
What does the "at" (#) symbol do in Python?
(14 answers)
Closed 2 years ago.
I was seeing the methods and docs of the built in super() method of python using the help() function in the IDLE .
I came across this piece of code
This works for class methods too: | class C(B): | #classmethod | def cmeth(cls, arg): | super().cmeth(arg)
In the second line , you can see the # sign before classmethod .
What does the # symbol does in python and what are its uses ?
The # character denotes a decorator. Decorators are functions that can modify or extend behavior of another function temporarily by wrapping around them.
Decorators wrap around a function by receiving them as a parameter. The # syntax (also known as "pie" syntax) applies the classmethod decorator to cmeth after it is defined in your snippet.
You can read more about the specific decorator from your example (classmethod) here.
Related
This question already has answers here:
What does -> mean in Python function definitions?
(11 answers)
Python3 function definition, arrow and colon [duplicate]
(3 answers)
What does the -> (dash-greater-than arrow symbol) mean in a Python method signature? [duplicate]
(1 answer)
Closed 9 months ago.
I'm analyzing some old code that I've inherited, and I have a question about the use of "self" and "None" keywords, specifically in the following example:
def run(self) -> None:
I understand that the self keyword is similar to the "this" keyword in C++ in that, in conjunction with the dot operator, it allows us to access the attributes and methods of the class in question. What I'm really interested in is the use of "-> None" in the declaration of the method named "run." Is this in PEP 8 because I can't find an example. I'm using Python 3.7, in case that matters.
What is the purpose of writing a method in this manner? What does "-> None" do?
They're called type hints, and they enable annotating the types of the parameters and return types of functions.
https://peps.python.org/pep-0484/
This question already has answers here:
Is it possible to escape a reserved word in Python?
(5 answers)
Closed 2 years ago.
Currently I am trying to implement an API in Python that originally was written in another programming language. This API has a function called except. I am trying to implement this function in Python but obviously this resulted in an error as except is already part of the Python language. It's probably bad practice, but does someone know if it's even possible to declare a function called except?
def except():
print('hi')
>>>
def except():
^
SyntaxError: invalid syntax
It's not possible; except is a Python keyword and cannot be used as a user-defined name in any way. You can find the complete list of keywords through the keyword module.
No it is a reserved keywords. They can't be used to declare a variable or a function.
You can't declare a method named "except" cause "except" is a reserved keyword in python but you can use "Except"
This question already has answers here:
Convert string to Python class object?
(10 answers)
Closed 4 years ago.
I checked I have a string whose content is a function name, how to refer to the corresponding function in Python? but it is not the question I want to ask.
The question is: I have a string 'ABC' and I want to create an instance of class ABC as:
my_obj = ABC ()
The input 'ABC' is read from Python arguments (argparse).
The class is already defined and imported.
You can use a dictionary:
d = {'ABC': ABC} # where ABC is the class object
my_obj = d['ABC']()
This should be preferable to solutions using global (poor practice) and eval (security risks).
This may seem cumbersome until you realise that classes and class instances are first class objects in Python. You should never rely on evaluating strings to call a Python object. Be explicit, even if it's not elegant.
This question already has answers here:
What does the "at" (#) symbol do in Python?
(14 answers)
Closed 8 years ago.
I'm currently learning Python and I came across a notation I was wondering about:
import taskmanager as tm
#........
#tm.task(str)
def feat(folder):
BLA BLA BLA ...
the code is a small excerpt from https://github.com/kfrancoi/phd-retailreco/blob/master/libraries/plsa/example_plsa.py (the file contains several more notations using the # sign).
Is this a common notation in python and what does it mean? Or is this only a notation used in this special case with the taskmanager or what!?
I tried my best looking this up on google but it's though to find as the #-sign is stripped out in my search (too short, special character). Same happens here on Stackoverflow.
Thank you very much in advance
This a decorator, defined by the PEP 318. Extract of the glossary:
A function returning another function, usually applied as a function transformation using the #wrapper syntax. Common examples for decorators are classmethod() and staticmethod().
The decorator syntax is merely syntactic sugar, the following two function definitions are semantically equivalent:
def f(...):
...
f = staticmethod(f)
#staticmethod
def f(...):
...
The same concept exists for classes, but is less commonly used there. See the documentation for function definitions and class definitions for more about decorators.
Related: What are some common uses for Python decorators?
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
python ‘self’ explained
Why do you need explicitly have the “self” argument into a Python method?
Why does Python require the "self" parameter for methods?
For example def method_abc(self, arg1)
And is there ever a date that the need for it will be removed?
Python gives you the option of naming it something other than self, even though the standard is to name it self. Just as it gives you the option of using tabs for indents, even though the standard is to use spaces.
In other words, it's not just "assumed" because...
To give you naming flexibility
To make it clearer that something will be passed self (or not).