-Memo # 1 for Python beginners to read "Detailed explanation of Python grammar"
--The bool
type that represents the result of a logical operation is also a numeric type object.
--Each Python data has its own type, but all are derived from the object
type.
--Any object is an instance of object
type, and all types are objects of type type
.
Built-in functions related to object attributes
>>> setattr(obj, 'ham', 100) # obj.ham =Same as 100
>>> getattr(obj, 'ham', 999) # try:
100 # return obj.ham
# except AttributeError:
# return 999
#Same as (default value can be omitted)
>>> delattr(obj, 'ham') # del obj.Same as ham
>>> hasattr(obj, 'ham') #Returns True if attribute name exists in obj
False
--Used to indicate "no value specified"
--Something like NULL
in C or null
in Java
--A singleton object of type NoneType
(the only object of this type)
Singleton object check process
# ==Is is faster than is, and moreover==Operators can be implemented independently on the object to be compared (judgment result cannot be guaranteed)
#(I'm not sure "I can implement it independently with the object to be compared")
>>> spam = 1
>>> spam == None #Not like this
False
>>> spam is None #like this
False
--Singleton object of type ellipsis
.
-"ellipsis" = It seems to mean omission. (I didn't know so I looked it up)
--Originally assumed to be used in third-party libraries that handle multidimensional arrays.
――The usage method and purpose of use have not been decided.
Example of using Ellipsis object
>>> ... #From Python3 it is possible to write with this literal
Ellipsis
#Represents the omitted part of the sample code
def spam():
...
#It may be good to indicate that the value is not specified explicitly
ham = ...
egg = (1, 2, ...)
Various ways to pass arguments
spam(0, 1, 2, 3, 4) #this is
args = [0, 1, 2, 3, 4] #Store it in an iterable object
spam(*args) #Can be written like this
spam(a = bacon, b = sausage) #This is
kwargs = {
"a":bacon,
"b":sausage
} #If you store it in a mapping object
spam(**kwargs) #Can be written like this
--In Python, functions, methods, classes, etc. are just a kind of objects that are the same as integers and strings.
--()
is also just an operator, so function call = just "apply the ()
operator to an object"
--The only difference between a function object and an integer object is __ whether it defines the behavior of the ()
operator or not __
--The object that defines the behavior of the ()
operator is called __callable object __
--One of the callable objects that creates an object of that type when a type object is also called
py:Because the method of the object is also a callable object...
>>> spam.ham(egg) #ham method call on spam object
>>> method = spam.ham #Get the ham attribute of the spam object
>>> method(egg) #If you write like this, you can call the method (wow)
Assigning to a variable means making it possible to refer to an object by variable name.
>>> spam = [1, 2, 3, 4] #Set the variable spam to reference a list object
>>> ham = spam #When assigned to another variable ham, ham also references the same object
>>> ham
[1, 2, 3, 4]
>>> spam.append('five') #When you add an element to a list object
>>> spam #Since both spam and ham refer to the same object,
[1, 2, 3, 4, 'five']
>>> ham #The displayed result is the same
[1, 2, 3, 4, 'five']
This is when a new object is created
>>> egg = bacon = 1
>>> egg = egg + 1 # egg +1 returns a new object
>>> egg
2
>>> bacon
1
Substitution all at once
>>> spam = [1, 2, 3]
>>> first, second, third = spam
>>> first, second, third #Decomposes and assigns elements of iterable objects
(1, 2, 3)
>>> (a, [b, c], d) = (1, (2, 3), 4) #The left side is[]Or()Can be further disassembled by surrounding with
>>> a, b, c, d
(1, 2, 3, 4)
When there are more elements on the right side than variables on the left side
# *If there is a variable with, it will be assigned to the list object in a nice way
>>> spam, ham, *egg = 1, 2, 3, 4, 5
>>> spam, ham, egg
(1, 2, [3, 4, 5])
>>> spam, *ham, egg = 1, 2, 3, 4, 5
>>> spam, ham, egg
(1, [2, 3, 4], 5)
>>> *spam, ham, egg = 1, 2, 3, 4, 5
>>> spam, ham, egg
([1, 2, 3], 4, 5)
When the number of elements on the right side is smaller than the variable on the left side
# *Variable with is an empty list with 0 elements
>>> spam, ham, *egg = 1, 2
>>> spam, ham, egg
(1, 2, [])
Recommended Posts