** * Caution Add what you noticed in sequence **
A java shop starts using python and lists the things that surprised me with the difference. Also, leave the coping method.
There is no switch syntax in python. It seems that the idea is that if there is no switch, it should be done with if. Alternatively, there is also a method using a dictionary.
Why is there no switch or case statement in Python? (Official site)
Method used in the dictionary
def function_1():
print('function_1')
def function_2():
print('function_2')
functions = {'a': function_1, 'b': function_2}
func = functions['b']
func()
↓ Execution result ↓
function_1
There are no strict constants in python By naming it in all capital letters, it is treated like a constant. It's just a variable, so of course you can change it if you like.
There is no interface in python. There is a concept of abstract classes called ABC (Abstract Base Classes), so I wonder if there is any particular problem. All you have to do is make it an abstract method.
How to write an abstract class(abctest.py)
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
class Cow(Animal):
def sound(self):
print("moo")
class Penguin(Animal):
pass #Does not implement abstract methods
if __name__ == "__main__":
hanako = Cow()
hanako.sound()
taro = Penguin() #I get an error when instantiating because I haven't implemented an abstract method
Execution result
moo
Traceback (most recent call last):
File "abctest.py", line 19, in <module>
taro = Penguin()
TypeError: Can't instantiate abstract class Penguin with abstract methods sound
There is no concept of overloading in python. Use the default argument settings and variadic arguments to do things like overloading.
This is no good
class Farm:
def add(self, animal):
print(animal)
def add(self, animal1, animal2):
print(animal1)
print(animal2)
if __name__ == "__main__":
farm = Farm()
farm.add('Dog')
farm.add('Jersey','Holstein')
↓ Execution result ↓
Traceback (most recent call last):
File "farmtest.py", line 13, in <module>
farm.add()
TypeError: add() missing 2 required positional arguments: 'animal1' and 'animal2'
class Farm:
def add(self, animal='Chicken'):
print(animal)
if __name__ == "__main__":
farm = Farm()
farm.add()
farm.add('Dog')
↓ Execution result ↓
Chicken
Dog
class Farm:
def add(self, *args):
if len(args) == 0:
print('blank')
for i in args:
print(i)
if __name__ == "__main__":
farm = Farm()
farm.add()
farm.add('Dog')
farm.add('Jersey','Holstein')
↓ Execution result ↓
blank
Dog
Jersey
Holstein
ApplicationContext
There is no Java Servlet or spring framework in django that corresponds to an ApplicationContext that is broader than the Session scope. Are you using django?
There is no labeled break in python to use when getting out of a double loop in java. I don't use it much in java, so it's okay.
python is valid in the function. java is valid in the block.
This is awkward for a java shop ...
Note that if you write it like a java instance variable, it will mean a class variable in python! If I made a mistake at first, I was surprised that the movement was unexpected.
class Cow:
type = 'Holstein' #Class variables
def __init__(self, name):
self.name = name #Instance variables
def walk(self):
print(self.name + 'Walk')
hanako = Cow('Hanako')
hanako.walk()
↓ Execution result ↓
Hanako walks
Recommended Posts