python predefined function (__ symbol in python) [duplicate] - python

This question already has answers here:
Why does python use two underscores for certain things? [duplicate]
(7 answers)
Closed 9 years ago.
Hello there i am very new to python (and i am from java background ) my question is does the predefined functions such as length, size or init (constructor of a class) starts with
__function__()
is this standard python syntax that every predefined should starts with this symbol ? ( for example touple.__len__() )? if not then whats the purpose of use this symbol
i am asking this quesion because this symbol confuse me a lot in python.

In python, method names which start and end in a double underscore have special meaning and are, by convention, reserved (there is nothing preventing you from creating your own "special" names, but you should rather not). For example, they are used to implement operator overloading.
You can find a nice introduction in A Guide to Python's Magic Methods. You can also see the Data model - Special method names section of the language reference.
You should also note that methods with an initial double underscore and no final double underscore are treated specially by the interpreter (they are name-mangled so that you get a "private-ish" method). This has nothing to do with methods starting and ending with a double underscore.

Something that begins and ends with a double underscore or often called a dunder, is a magic function or method. Usually, they make objects work with operators or base functions (like len or statements like del, so basically functions or statements of importance).
So, for example:
__add__ is for the + operator
__mul__ is for * operator.
So, if you have two objects of the same type say Foo, then Foo('a') + Foo('b'), would call Foo('a').__add__(Foo('b')).
They behave as reserved words and have some protection. So, for example:
class Foo(object):
def __init__(self):
self.x = 10
def __len__(self):
return "Cheese"
if __name__ == '__main__':
a = Foo()
print a.__len__()
would print out Cheese to the console. However, this would give you a TypeError:
if __name__ == '__main__':
a = Foo()
print len(a)

the __ and _ symbol is for "private" functions
usually you do not use function name with _. functions that start with _ are usually python's and are being used so dont override them or something if you dont know what you are doing
if you want to make a function "private" you use _. e.g : def _myFunc()

Related

Using a list comprehension and string formatting on class properties [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 10 months ago.
Here is the basic class code:
class test:
for i in range(10):
exec(f'a{i}={i+1}')
My first attempt to create the list a_all was to do the following:
exec(f'a_all=[test.ak for k in range(0,10,2)]')
However, this doesn't work as exec does not 'unstring' ak the way I want it to. Other attempts failed because the exec function returns None. I'm not even confident anymore that exec could actually work.
Your non-listcomp code works as written (kinda surprised by that), defining attributes of test, so just use that if it's what you need (it's almost certainly a bad idea, but you do you).
This doesn't work in your listcomp code because:
test doesn't exist until the class definition completes (after you dedent at the end), so test.ANYTHING doesn't work inside the definition of test (you assign to top-level names, and they become attributes of test when the class finishes being defined, but they're just scoped names without test. qualification before then)
Omitting test doesn't work in Python 3 because listcomps have their own scope in Python 3; even if you fake it out to assign to each of your names in turn, the iteration variables don't leak to outer scope on Python 3.
In any event, what you're doing is nonsensical:
Your plain for loop code works, but
It's a bad idea; just have a single class attribute of the list and have users index it as needed. There's basically never a good reason to have a programmatically generated list of numbered variable names. That's reinventing lists, badly.
So just use:
class test:
a = range(0, 10, 2) # Or list(range(0, 10, 2)) if it must be mutable
and user's can do test.a[0], test.a[3], whatever they need.
You can use setattr and getattr to set class attributes.
class test:
def __init__(self):
for i in range(10):
setattr(self, f"a{i}", i + 1)
t = test()
for i in range(10):
print(f"{t.a1=} a{i}={getattr(t, f'a{i}')=}")

What does '_' mean in python [duplicate]

What is the meaning of _ after for in this code?
if tbh.bag:
n = 0
for _ in tbh.bag.atom_set():
n += 1
_ has 3 main conventional uses in Python:
To hold the result of the last executed expression in an interactive
interpreter session (see docs). This precedent was set by the standard CPython
interpreter, and other interpreters have followed suit
For translation lookup in i18n (see the
gettext
documentation for example), as in code like
raise forms.ValidationError(_("Please enter a correct username"))
As a general purpose "throwaway" variable name:
To indicate that part
of a function result is being deliberately ignored (Conceptually, it is being discarded.), as in code like:
label, has_label, _ = text.partition(':')
As part of a function definition (using either def or lambda), where
the signature is fixed (e.g. by a callback or parent class API), but
this particular function implementation doesn't need all of the
parameters, as in code like:
def callback(_):
return True
[For a long time this answer didn't list this use case, but it came up often enough, as noted here, to be worth listing explicitly.]
This use case can conflict with the translation lookup use case, so it is necessary to avoid using _ as a throwaway variable in any code block that also uses it for i18n translation (many folks prefer a double-underscore, __, as their throwaway variable for exactly this reason).
Linters often recognize this use case. For example year, month, day = date() will raise a lint warning if day is not used later in the code. The fix, if day is truly not needed, is to write year, month, _ = date(). Same with lambda functions, lambda arg: 1.0 creates a function requiring one argument but not using it, which will be caught by lint. The fix is to write lambda _: 1.0. An unused variable is often hiding a bug/typo (e.g. set day but use dya in the next line).
The pattern matching feature added in Python 3.10 elevated this usage from "convention" to "language syntax" where match statements are concerned: in match cases, _ is a wildcard pattern, and the runtime doesn't even bind a value to the symbol in that case.
For other use cases, remember that _ is still a valid variable name, and hence will still keep objects alive. In cases where this is undesirable (e.g. to release memory or external resources) an explicit del name call will both satisfy linters that the name is being used, and promptly clear the reference to the object.
It's just a variable name, and it's conventional in python to use _ for throwaway variables. It just indicates that the loop variable isn't actually used.
Underscore _ is considered as "I don't Care" or "Throwaway" variable in Python
The python interpreter stores the last expression value to the special variable called _.
>>> 10
10
>>> _
10
>>> _ * 3
30
The underscore _ is also used for ignoring the specific values. If you don’t need the specific values or the values are not used, just assign the values to underscore.
Ignore a value when unpacking
x, _, y = (1, 2, 3)
>>> x
1
>>> y
3
Ignore the index
for _ in range(10):
do_something()
There are 5 cases for using the underscore in Python.
For storing the value of last expression in interpreter.
For ignoring the specific values. (so-called “I don’t care”)
To give special meanings and functions to name of variables or functions.
To use as ‘internationalization (i18n)’ or ‘localization (l10n)’ functions.
To separate the digits of number literal value.
Here is a nice article with examples by mingrammer.
As far as the Python languages is concerned, _ generally has no special meaning. It is a valid identifier just like _foo, foo_ or _f_o_o_.
The only exception are match statements since Python 3.10:
In a case pattern within a match statement, _ is a soft keyword that denotes a wildcard. source
Otherwise, any special meaning of _ is purely by convention. Several cases are common:
A dummy name when a variable is not intended to be used, but a name is required by syntax/semantics.
# iteration disregarding content
sum(1 for _ in some_iterable)
# unpacking disregarding specific elements
head, *_ = values
# function disregarding its argument
def callback(_): return True
Many REPLs/shells store the result of the last top-level expression to builtins._.
The special identifier _ is used in the interactive interpreter to store the result of the last evaluation; it is stored in the builtins module. When not in interactive mode, _ has no special meaning and is not defined. [source]
Due to the way names are looked up, unless shadowed by a global or local _ definition the bare _ refers to builtins._ .
>>> 42
42
>>> f'the last answer is {_}'
'the last answer is 42'
>>> _
'the last answer is 42'
>>> _ = 4 # shadow ``builtins._`` with global ``_``
>>> 23
23
>>> _
4
Note: Some shells such as ipython do not assign to builtins._ but special-case _.
In the context internationalization and localization, _ is used as an alias for the primary translation function.
gettext.gettext(message)
Return the localized translation of message, based on the current global domain, language, and locale directory. This function is usually aliased as _() in the local namespace (see examples below).

Understanding "def main" and "overloading" in Python [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I often see some code with
def main(A,B)
some steps
described as an "overloading for the main function", after reading something more specific about Python I know that this is not true because:
Python is a loseless type language
a function/method in Python doesn't even know the type for a given parameter, nor python cares
I'm also not sure if there is a real distinction between instances and "static methods" or "class methods", probably they are the same thing and the only difference is given by the use of decoratos, but this is probably related to my first approach to a functional language after spending so much time with C/C++ .
Even more, in Python, the first indentation level is used as an entry point ( like main() in C/C++ ), so I don't get why you should define a main at an indentation level that is different from the really first one.
The general idea that I have about Python's keywords is that the keywords have a special semantic value rather than a real defined syntax ( probably it's because the C++ is less "idiomatic" , I don't really know how explain that ), they are used as a placeholder for something and they give a special meaning to the section where they are applied. There also special variables like __name__ that are there just to store special informations and nothing more.
After all this small bits of information stored in my brain I still dont' get the real meaning of the first code example:
what is special about the main() function ?
what is the point of defining something like a "main" function if it's not real "main"
how Python decide what function is the one to call if it's not typed ?
there is a difference between how the interpreter reads __init__.py files and other files ?
To answer your questions:
main() function
Nothing is special about it. You have to call it yourself, by doing something like:
def main():
print "This is main"
if __name__ == "__main__":
main()
Why use main()
The reason you might do that is to keep your main entry-point code together in a convenient way. For instance, if you create some variables in main(), they won't be global variables, which avoids polluting the global namespace. It also prevents the main() function from being run if you import the module from another (instead of running it directly as a script). This can be useful if you don't want to do some initialization (e.g., print messages) when importing, but you do want to do them when running as a standalone script.
How does Python decide which function to call
Python does not support "overloading" in this sense. There can only be one function with a given name in a given namespace. If you make a second function with the same name (or second method with the same name in the same class), you overwrite the first one completely.
__init__.py
This question is not really related to your others. But no, it doesn't process them in a different way. It processes them at a different time (when you import a package, rather than a module in a package), but the code in them is run just the same as the code in any other Python file.
main() in python is just a function name. The common idiom
if __name__ == '__main__':
#do something
is a shortcut to figure out of the code in this file is being run as a program rather than imported as a module.
Because python is intended to by type-free, the community puts a strong emphasis on conventions and best practices. This doesn't provide the same level of predictability as a compiler, but it helps keep the chaos at bay. Readability is a core python value and idioms like this provide valuable structure.
Python does not support function overloading in the sense that other languages do. In strongly typed languages you might write multiple versions of the same function with the same name but varying input arguments:
void DoSomething (int a) {};
void DoSomething (float f) {};
void DoSomething (string s){};
In python there is no equivalent idiom. In most cases it's unneeded: for a numeric operation you don't really care if the incoming numbers are floats, ints or whatever -- only that they support the correct operators. This is where the python mantra of 'duck typing' comes in - if it walks like a duck and quacks like a duck, it's a duck. So python functions idiomatically look for functions or operators on incoming arguments rather than checking their types.
As for instance vs static methods:
In python every instance method implicitly gets the owning instance as the first argument to a function:
class Test(object):
def __init__(self, arg):
self.variable = arg
def example(self):
print self.variable
fred = Test(999) # note: no 'self' passed here
fred.example()
>>> 999
joe = Test(-1)
joe.example()
>>> -1
a class method gets the class type, rather than an instance, as the implicit first argument. Class methods can see class-level variables, which are defined in the class scope rather than in the instance scope - but they don't know anything about a particular instance.
class TestCls (Test)
CLASS_VARIABLE = "hello"
# other behavior inherited from Test
#classmethod
def class_example(cls):
print cls.CLASS_VARIABLE
barney = TestCls(123)
barney.example()
>>> 123
barney.class_example() # again, no explicit class passed in
>>> 'hello'
a static method gets no implicit arguments at all:
class TestStatic (TestCls):
CLASS_VARIABLE = 'goodbye'
# inherited stuff again
#staticmethod
def static_test():
print "behold, I have no implicit argument"
Static and class methods also don't need an instance to be called:
wilma = TestStatic(123)
wilma.static_test() # you can call from an instance
>>> behold, I have no implicit argument
# or not:
TestStatic.static_test()
>>> behold, I have no implicit argument
TestStatic.class_example()
>>> goodbye # the method is inherited, but the class variable come from this class

Can fabric tasks have names that are not valid Python function names?

I'd like my Fabric tasks to have hyphens (-) instead of underscores (_). For example, database-reset instead of database_reset. However, hyphens aren't permitted as Python function names.
Is it possible in Fabric to create tasks whose names do not exactly match the corresponding Python function?
From the documentation:
#task(alias = 'database-reset')
def database_reset():
...
Cat Plus Plus has the best solution to what you want to do. Tangentially, however, it is technically possible to have Python global variables (a function name is just a global variable) that don't conform to the usual rules by assigning to the globals() dictionary.
def foo_bar():
print "foo-bar"
globals()["foo-bar"] = foo_bar
globals()["foo-bar"]() # prints "foo-bar"
The syntax is not very nice, though, making it quite the hassle.

Why does python disallow usage of hyphens within function and variable names?

I have always wondered why can't we use hyphens in between function names and variable names in python
Having tried functional programming languages like Lisp and Clojure, where hyphens are allowed. Why python doesn't do that.
# This won't work -- SyntaxError
def is-even(num):
return num % 2
# This will work
def is_even(num):
return num % 2
I am sure Sir Guido must have done this because of some reasons. I googled but couldn't manage to find the answer. Can anyone please throw some light on this?
Because hyphen is used as the subtraction operator. Imagine that you could have an is-even function, and then you had code like this:
my_var = is-even(another_var)
Is is-even(another_var) a call to the function is-even, or is it subtracting the result of the function even from a variable named is?
Lisp dialects don't have this problem, since they use prefix notation. For example, there's clear difference between
(is-even 4)
and
(- is (even 4))
in Lisps.
Because Python uses infix notation to represent calculations and a hyphen and a minus has the exact same ascii code. You can have ambiguous cases such as:
a-b = 10
a = 1
b = 1
c = a-b
What is the answer? 0 or 10?
Because it would make the parser even more complicated. It would be confusing too for the programmers.
Consider def is-even(num): : now, if is is a global variable, what happens?
Also note that the - is the subtraction operator in Python, hence would further complicate parsing.
is-even(num)
contains a hyphen ? I thought it was a subtraction of the value returned by function even with argument num from the value of is.
As #jdupont says, parsing can be tricky.
Oddly enough it is possible to have class variable names with hyphens using setattr(), not that you would want to. Here is an example:
class testclass:
pass
x = testclass()
setattr(x, "is-even", True)
getattr(x, "is-even")
True
This still fails:
x.is-even
File "<stdin>", line 1
x.is-even
^
SyntaxError: invalid syntax

Categories