我作为前端开发人员已经工作了 8 年多,在过去的 2 年里,我决定重新思考我的职业生涯以及如何成长。我发现前端技术变化频繁:不同的框架、概念以及React类组件和钩子之间的差距。我意识到这一切只是用来表达业务需求和个人愿景的抽象。从那时起,我决定改变自己的职业道路,稍微成为一名全栈开发人员。
众所周知,现在的前端开发都是JavaScript,这也是我决定学习Node.js及其主要框架的原因。所有前端开发人员都会以某种方式遇到 Node.js,作为一名高级前端开发人员,您应该能够使用 Express 或其他库在 Node.js 中编写基本端点。经过两年在 Node.js 方面的积极开发,当我的工作在前端和后端之间达到 50/50 时,我发现大多数项目并不局限于一种语言。
Node.js 并不是万能的理想工具,尤其是当您在一家较大的公司工作时。不同的语言提供不同的解决方案或者更适合解决特定的业务案例。这就是为什么我开始研究我可以学习什么作为我的第二后端语言以及我将来如何利用它。
我想分享我的经验以及为什么我在尝试学习 Rust(主要不是用于 Web 开发)、Swift(主要是移动优先解决方案)和 Golang 后决定坚持使用 Python。在这里,您将了解为什么我相信 Python 是前端开发人员学习以及如何开始使用它的绝佳机会。
如今,人工智能是每个人都在谈论的话题。在采访中提及它作为你的经历的一部分总是会给你加分。几乎所有公司都在尝试将人工智能融入到他们的产品中,而Python与人工智能和机器学习齐头并进。通过学习 Python,您不仅有机会使用 Django、Flask 和 FastAPI 等框架编写 Web 应用程序,还可以开始使用机器学习和 AI 服务。
一方面,如果你想成为一名更好的程序员,学习 Rust、Go 或 Elixir 等更复杂的语言是个好主意。然而,从职业角度来看,要成为一名使用自己不太熟悉的完全不同语言的后端开发人员并不是一件容易的事。
Python 是一种动态类型编程语言。作为一名在类似环境中度过了职业生涯的大部分时间的 JavaScript 开发人员,这不应该让您感到害怕,因为您已经知道代码中会出现哪些类型的问题。
使用Python,您不仅可以开始编写Web应用程序,还可以利用您在AI相关领域的技能,这使Python作为第二语言具有显着的优势。
学习曲线很简单。在Python中,你肯定需要学习一些基本概念。如果您有 JavaScript 经验,那应该不是什么大问题。
以下是 Python 代码示例:
import random def guess_the_number(): number_to_guess = random.randint(1, 100) attempts = 0 guessed = False print("Welcome to the Number Guessing Game!") print("I'm thinking of a number between 1 and 100. Can you guess what it is?") while not guessed: user_guess = int(input("Enter your guess: ")) attempts += 1 if user_guess < number_to_guess: print("Too low! Try again.") elif user_guess > number_to_guess: print("Too high! Try again.") else: print(f"Congratulations! You guessed the number {number_to_guess} in {attempts} attempts.") guessed = True guess_the_number()
我认为你不会在这里发现任何太复杂的东西。即使你之前没有学过Python,你也可以在不阅读文档的情况下理解几乎所有的行。
您会注意到的最大区别是 Python 使用缩进来定义代码块而不是花括号。这可能看起来很奇怪,我仍然觉得它有点不寻常,但过了一段时间,你就会习惯它,并且阅读代码变得更容易。
除此之外,Python 中的许多概念与 JavaScript 中的相似。您可以使用 print 来代替 console.log,对于字符串插值,您可以在字符串的开头添加 f 并使用与 JavaScript 中几乎相同的语法。
这是上面代码的 JavaScript 版本:
function guessTheNumber() { const numberToGuess = Math.floor(Math.random() * 100) + 1; let attempts = 0; let guessed = false; console.log("Welcome to the Number Guessing Game!"); console.log("I'm thinking of a number between 1 and 100. Can you guess what it is?"); while (!guessed) { const userGuess = parseInt(prompt("Enter your guess: "), 10); attempts++; if (userGuess < numberToGuess) { console.log("Too low! Try again."); } else if (userGuess > numberToGuess) { console.log("Too high! Try again."); } else { console.log(`Congratulations! You guessed the number ${numberToGuess} in ${attempts} attempts.`); guessed = true; } } } guessTheNumber();
你可以在Python中学习很多不同的概念。作为一名 JavaScript 开发者,我展示了最令人困惑的部分。
作为一名 JavaScript 开发人员,您可能熟悉如何使用带有 if/else 和其他运算符的代码块。在大多数情况下,您只需添加 {} 即可。 Python 基于缩进的系统可能很棘手。
让我们看看 JavaScript 代码:
if (role === "admin") { const posts = getDraftPosts() if (posts.length === 0) { throw NotFound() } return posts }
Python 模拟:
if role == "admin": posts = get_draft_posts() if posts.length == 0: raise NotFound() return posts
As you can see readability of blocks in Python could be challenging from the first view. This is why for me it was important to avoid deeply nested blocks because it is hard to read and easy to miss correct indention. Keep in mind that Python could attach your code to a wrong code block because of missed indention.
Python is a dynamic typing language but I was surprised to find Python built-in Types annotation.
def add(x: int, y: int) -> int: return x + y
You don’t need to install additional features, only what you need in Python *3.5 and above.*
Even more complex types could be described as equal to Typescript:
# enums from enum import Enum # import enum for built in lib class Season(Enum): # extend class to mark it as enum SPRING = 1 SUMMER = 2 AUTUMN = 3 WINTER = 4 print(Season.SPRING.name) # SPRING print(Season.SPRING.value) # 1 # or generics def first(container: List[T]) -> T: return container[0] list_two: List[int] = [1, 2, 3] print(first(list_two)) # 1
For using these types you are not required to install something or transpile this code. This is something I missed in JavaScript, at least Node.js. I know Node.js is adding some basic types in the nearest version (see Medium post about node.js built-in types support) but it looks poor now if you compare it with Python.
JavaScript uses an event-driven, non-blocking model, while Python's Global Interpreter Lock (GIL) can be a confusing concept in multi-threaded programs.
The Python Global Interpreter Lock (GIL) is a mechanism that ensures only one thread executes Python code at a time. Even if your Python program has multiple threads, only one thread can execute Python code at a time due to the GIL.
With JavaScript, you can achieve multithreading with web workers, but in Python, you need to use additional libraries to accomplish this.
JavaScript's "multiple ways to do it" philosophy doesn't work as well in Python because Python adheres more closely to the concept "There should be one - and preferably only one - obvious way to do it."
In the JavaScript world, every company often creates its own code style guide, and it's fortunate if it follows basic JavaScript style recommendations. In reality, practices like using semicolons can vary widely, to the point where one project might use semicolons and another might not.
In Python, following Pythonic principles from PEP 8 (Python's style guide) is highly recommended. This guide outlines the basic rules of how to write Python code.
To write better code, it's essential to engage with the community and learn idiomatic Python practices that prioritize clarity and simplicity.
This part might also be confusing. In JavaScript, you can usually add a package manager and start installing dependencies without any issues. However, Python’s pip and virtual environments may be new concepts.
In Python, when using additional dependencies, it’s highly recommended to use a separate virtual environment. Installing dependencies with pip (the Python equivalent of npm in JavaScript) in your environment could potentially break system utilities or the OS itself, as the system Python interpreter (the one that comes pre-installed with your operating system) is used by the OS and other system utilities.
As a solution, you can create a virtual environment with venv module:
python -m venv myenv myenv\Scripts\activate # for windows source myenv/bin/activate # for Mac
After you enter the virtual environment you can install all dependencies without any problem or danger for your root environment.
Learning a new language is always challenging. I started learning Python basics on an online platform, where I also completed some small projects. Here is the plan I used during my learning process:
You can find a lot of help in Reddit communities or Discord servers while learning. I’m mostly a Reddit user and would suggest finding subreddits for Python and the framework you decide to use for your first application.
Remember to use the official documentation. In my opinion, it looks overwhelming, and most of the time, I try to find related articles if I get stuck on a concept.
Make sure to read PEP 8 — Style Guide for Python Code, where you can find basic rules on how to write Python code.
当我回顾自己从 JavaScript 开发人员到拥抱 Python 的旅程时,我并不后悔。这一转变带来了令人兴奋的机会,特别是在人工智能和机器学习领域,我现在在我的项目中广泛利用这些机会,特别是在后端。
展望未来,Python 的可能性是巨大的。无论是 Web 开发、数据科学、自动化,还是深入研究人工智能和机器学习,Python 都提供了强大且多功能的基础来构建和探索新视野。
以上是全栈开发:作为 JavaScript 开发人员学习 Python的详细内容。更多信息请关注PHP中文网其他相关文章!