Python Is Not Java

使用Python中的tabula模块进行pdf2excel转化时出现JAVA_NOT_FOUND_ERROR问题 使用Python中的tabula模块进行pdf2excel转化时出现JAVA_NOT_FOUND_ERROR问题1、python中pdf2excel转化模块的选用1.1 pdfminer1.2 pdfplumber1.3 tabula2 python中tabula模块的安装及问题解决2.1 tabula安装2.2 如果,使用pip成功安装tabula-py后在python和anaconda中都导入tabula失败解决方法:2.3 如果,在python和anaconda中执行tabula.read_pdf() 阅读详情

Python Is Not Java

 

I was recently looking at the source of a wxPython-based GUI application, about 45.5KLOC in size, not counting the libraries used (e.g. Twisted). The code was written by Java developers who are relatively new to Python, and it suffers from some performance issues (like a 30-second startup time). In examining the code, I found that they had done lots of things that make sense in Java, but which suck terribly in Python. Not because "Python is slower than Java", but because there are easier ways to accomplish the same goals in Python, that wouldn't even be possible in Java.

So, the sad thing is that these poor folks worked much, much harder than they needed to, in order to produce much more code than they needed to write, that then performs much more slowly than the equivalent idiomatic Python would. Some examples:
  • A static method in Java does not translate to a Python classmethod. Oh sure, it results in more or less the same effect, but the goal of a classmethod is actually to do something that's usually not even possible in Java (like inheriting a non-default constructor). The idiomatic translation of a Java static method is usually a module-level function, not a classmethod or staticmethod. (And static final fields should translate to module-level constants.)

    This isn't much of a performance issue, but a Python programmer who has to work with Java-idiom code like this will be rather irritated by typing Foo.Foo.someMethod when it should just be Foo.someFunction. But do note that calling a classmethod involves an additional memory allocation that calling a staticmethod or function does not.

    Oh, and all those Foo.Bar.Baz attribute chains don't come for free, either. In Java, those dotted names are looked up by the compiler, so at runtime it really doesn't matter how many of them you have. In Python, the lookups occur at runtime, so each dot counts. (Remember that in Python, "Flat is better than nested", although it's more related to "Readability counts" and "Simple is better than complex," than to being about performance.)

  • Got a switch statement? The Python translation is a hash table, not a bunch of if-then statments. Got a bunch of if-then's that wouldn't be a switch statement in Java because strings are involved? It's still a hash table. The CPython dictionary implementation uses one of the most highly-tuned hashtable implementations in the known universe. No code that you write yourself is going to work better, unless you're the genetically-enhanced love child of Guido, Tim Peters, and Raymond Hettinger.

  • XML is not the answer. It is not even the question. To paraphrase Jamie Zawinski on regular expressions, "Some people, when confronted with a problem, think "I know, I'll use XML." Now they have two problems."

    This is a different situation than in Java, because compared to Java code, XML is agile and flexible. Compared to Python code, XML is a boat anchor, a ball and chain. In Python, XML is something you use for interoperability, not your core functionality, because you simply don't need it for that. In Java, XML can be your savior because it lets you implement domain-specific languages and increase the flexibility of your application "without coding". In Java, avoiding coding is an advantage because coding means recompiling. But in Python, more often than not, code is easier to write than XML. And Python can process code much, much faster than your code can process XML. (Not only that, but you have to write the XML processing code, whereas Python itself is already written for you.)

    If you are a Java programmer, do not trust your instincts regarding whether you should use XML as part of your core application in Python. If you're not implementing an existing XML standard for interoperability reasons, creating some kind of import/export format, or creating some kind of XML editor or processing tool, then Just Don't Do It. At all. Ever. Not even just this once. Don't even think about it. Drop that schema and put your hands in the air, now! If your application or platform will be used by Python developers, they will only thank you for not adding the burden of using XML to their workload.

    (The only exception to this is if your target audience really really needs XML for some strange reason. Like, they refuse to learn Python and will only pay you if you use XML, or if you plan to give them a nice GUI for editing the XML, and the GUI in question is something that somebody else wrote for editing XML and you get to use it for free. There are also other, very rare, architectural reasons to need XML. Trust me, they don't apply to your app. If in doubt, explain your use case for XML to an experienced Python developer. Or, if you have a thick skin and don't mind being laughed at, try explaining to a Lisp programmer why your application needs XML!)

  • Getters and setters are evil. Evil, evil, I say! Python objects are not Java beans. Do not write getters and setters. This is what the 'property' built-in is for. And do not take that to mean that you should write getters and setters, and then wrap them in 'property'. That means that until you prove that you need anything more than a simple attribute access, don't write getters and setters. They are a waste of CPU time, but more important, they are a waste of programmer time. Not just for the people writing the code and tests, but for the people who have to read and understand them as well.

    In Java, you have to use getters and setters because using public fields gives you no opportunity to go back and change your mind later to using getters and setters. So in Java, you might as well get the chore out of the way up front. In Python, this is silly, because you can start with a normal attribute and change your mind at any time, without affecting any clients of the class. So, don't write getters and setters.

  • Code duplication is quite often a necessary evil in Java, where you must often write the same method over and over with minor variations (usually because of static typing constraints). It is not necessary or desirable to do this in Python (except in certain rare cases of inlining a few performance-critical functions). If you find yourself writing the same function over and over again with minor variations, it's time to learn about closures. They're really not that scary.

    Here's what you do. You write a function that contains a function. The inner function is a template for the functions that you're writing over and over again, but with variables in it for all the things that vary from one case of the function to the next. The outer function takes parameters that have the same names as those variables, and returns the inner function. Then, every place where you'd otherwise be writing yet another function, simply call the outer function, and assign the return value to the name you want the "duplicated" function to appear. Now, if you need to change how the pattern works, you only have to change it in one place: the template.

    In the application/platform I looked at, just one highly trivial application of this technique could have cut out hundreds of lines of deadweight code. Actually, since the particular boilerplate has to be used by developers developing plugins for the platform, it will save many, many more hundreds of lines of third-party developer code, while simplifying what those developers have to learn.
This is only the tip of the iceberg for Java->Python mindset migration, and about all I can get into right now without delving into an application's specifics. Essentially, if you've been using Java for a while and are new to Python, do not trust your instincts. Your instincts are tuned to Java, not Python. Take a step back, and above all, stop writing so much code.

To do this, become more demanding of Python. Pretend that Python is a magic wand that will miraculously do whatever you want without you needing to lifting a finger. Ask, "how does Python already solve my problem?" and "What Python language feature most resembles my problem?" You will be absolutely astonished at how often it happens that thing you need is already there in some form. In fact, this phenomenon is so common, even among experienced Python programmers, that the Python community has a name for it. We call it "Guido's time machine", because sometimes it seems as though that's the only way he could've known what we needed, before we knew it ourselves.

So, if you don't feel like you're at least ten times more productive with Python than Java, chances are good that you've been forgetting to use the time machine! (And if you miss your Java IDE, consider the possibility that it's because your Python program is much more complex than it needs to be.)

 from http://dirtsimple.org/2004/12/python-is-not-java.html
Python 和 Java 中 null 值、集合、空字符串、空格字符串的判断详解 在布尔上下文中,空字符串被视为 False,因此使用 not value 可以简洁地判断字符串是否为空。isEmpty() 方法检查字符串长度是否为零,equals(“”) 比较字符串内容是否为空字符串。在编程中,判断变量是否为 null、集合是否为空,以及字符串是否为空或仅包含空格是非常常见的操作。trim() 方法移除字符串两端的空白字符,如果结果为空字符串,则说明原字符串为空或仅包含空格。strip() 方法移除字符串两端的空白字符,如果结果为空字符串,则说明原字符串仅包含空格。 阅读详情

相关推荐

NRF24L01模块的5个常见问题及解决方案:从信号干扰到功耗优化

本文深入探讨NRF24L01无线模块在STM32开发中的5大常见问题及解决方案,包括通信距离优化、数据丢失排查、电源噪声抑制、SPI通信故障处理以及低功耗设计。通过代码示例和电路图分析,提供实用的优化策略,帮助开发者提升无线通信的稳定性和效率。

weixin_28256233的博客 226

java command not found 问题出现和解决

在启动定时任务的时候。有时会出现java command not found的错误提示,是找不到JAVA环境变量所导致的。在此记录,防止遗忘。 在任意目录下执行 export JAVA_HOME=/home/uat_ops/java/jdk_default export PATH=$JAVA_HOME/bin:$PATH export CLASSPATH=.:$JAVA_HOME/lib/dt

马路上开飞机 3万+

西门子200SMART PID温控实战:从配置到避坑(附加热棒控制案例)

本文详细介绍了西门子200SMART PLC在PID温控系统中的实战应用,涵盖硬件配置、PID参数整定及工程避坑技巧。通过加热棒控制案例,重点解析了信号处理、固态继电器选型、断电保持和无扰切换等关键问题,为工业自动化领域的温度控制提供可靠解决方案。

weixin_28727975的博客 356

java command not found 问题出现和解决

在kettle中运行脚本,脚本里需要运行jar包,java -jar XXXX.jar 然后运行日志报错显示java command not found 说明在kettle中运行脚本时,找不到java的路径,需要在运行脚本中加上如下三行即可: export JAVA_HOME=/home/hdp_teu_dpd/user/wangxuan/wmda/jdk1.8.0_131 export ...

攻城狮Kevin 1万+

防火墙学习5---虚拟系统和跟系统通过路由表互访

虚拟系统与跟系统互访可分为虚拟系统访问根系统和跟系统访问虚拟系统。

weixin_48030849的博客 1031

ZYNQ裸机LWIP双网口实现

文章目录前言一、LWIP双网口二、使用步骤1.部分说明2.部分代码总结 前言 一、LWIP双网口 由于项目需要多通讯口,查阅资料发现lwip支持多网口操作。于是,就开搞了。 二、使用步骤 1.部分说明 利用xemac_add函数在网卡链表中添加一个网卡,即使用netif块重复xemac_add操作。 先用轮询的方式实现双网口,后期修改为中断模式 2.部分代码 导入官方例程LWip echo server, 修改main函数,增加一个netif块 #if LWIP_IPV6==0 #if L..

weixin_35865280的博客 1万+

-bash: java: command not found 问题处理

在vi /etc/profile 编辑完文件后,还需使用命令。然后输入java -version进行测试成功。在Linux系统上安装java环境时,使用。

LP'S Blog 1万+

那些年-SAP固定资产导入AS91 OASV

固定资产起初导入步骤: 第一部分:资产数据导入 1)两个日期配置 填写图片摘要(选填) Specify transfer date/last closed fiscal year 这里的日期填写系统切换日期,如果系统是2015-10-01上线,这里填写2015-09-3 填写图片摘要(选填) Specifylast period posted in prv system 这里指定系统切换的上一期间。这里写2015-09 填写图片摘要(选填) ​2)AS91新增 填写固定资产的分类,公司

阿土工作室 7947

Python命令行command not found

问题 当命令行输入命令python时返回 command not found 处理 1. 安装python 如果没有安装python,需要下载 python, 下载完成之后安装,并记住安装地址 2.配置环境变量 【电脑】-> 【属性】->【高级系统设置】->【环境变量】,找到【path】并配置 python安装地址 最后保存 3. 测试python命令是否正常 cmd命令行 python回车,能够输入命令即正常 ...

wzp20092009的博客 1万+

SpringBoot Actuator未授权访问漏洞实战:从发现到利用

本文深入剖析了SpringBoot Actuator未授权访问漏洞的成因、危害与利用手法。文章详细演示了如何发现暴露的端点,并通过实战案例复现了从敏感信息泄露到利用反序列化漏洞实现远程代码执行(RCE)的全过程,为开发与运维人员提供了从漏洞发现到修复加固的完整安全指南。

weixin_29231221的博客 260

Python爬虫实战:如何从Google Maps和OpenStreetMap抓取地理坐标数据

地点名称:地理位置的名称或地址。地理坐标:经度和纬度。类别信息:比如餐馆、商店、公园等。评论数据:如果可用,获取用户评论。本文介绍了如何使用Python爬虫从Google Maps和OpenStreetMap抓取地理坐标数据,并展示了如何存储和分析这些数据。通过实际的代码示例,您可以学习到如何处理不同类型的地图数据抓取任务,以及如何应对反爬虫机制。掌握这些技术后,您可以进行更广泛的地理数据分析,提供更有价值的地理信息服务。

2201_76125261的博客 1039

Java Is Not Python, either...

                Java is not Python, either...       在我发表了"Python Is Not Java"这篇文章后,大家对此产生了热烈的响应,我想我确实也需要再拿出一些时间来阐述Java Is Not Python,either. Java是一个功能非常强大的语言,在JDBC、JNDI、OSGi等很多方面都制定了标准和规范.       当然,

Phillip J.Eby 博客中文版 4265

【视频监控国标GB/T28181】无人机的视频图传技术

然而,随着无人机市场的不断发展和无人机视频图传技术的迅速进步,一些厂家正积极探索更多的传输方式。如今,一些大疆无人机已经具备了4G辅助图传功能,这意味着它们需要在无人机上安装相应的SIM卡,并在有移动互联网信号的情况下,通过互联网进行视频图传。这种技术的应用,不仅提高了无人机的操作灵活性,也使得无人机在各个领域的应用更加广泛。在使用时,用户需时刻注意无人机的飞行距离。在无人机图传系统中,OFDM技术的运用更是如虎添翼,显著增强了视频传输的品质与稳定性,尤其在错综复杂的电磁环境中,更能凸显其不可替代的优势。

开源物联网项目商业化作者 5662

Python项目新建包的时候出现Project is not a Java project的错误

经百度后找到如下解决方案: 项目名称=》属性=》Project Facets=》java 经过以上两个步骤后:项目中就出现了src的source folder文件夹

paradisesunny的专栏 1372

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

Frida 17 以后 Python API 跑旧版 JS 报 Java is not defined ?一行 import 直接恢复 Frida 16 体验

针对 Frida 17.0.0 以上版本中 Python API 运行旧版 JS 脚本时出现的 bridge 失效问题,本文给出一个开箱即用的解决方案: frida-legacy-compat 。它可以自动处理 bridge 和编译流程,让开发者继续用最熟悉的 session.create_script(source) 注入方式完成 Hook。

qq_34511096的博客 715

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

python 调用jar 获取返回值报错,jpype._core.JVMNotRunning: Java Virtual Machine is not running

python 调用jar 获取返回值报错,jpype._core.JVMNotRunning: Java Virtual Machine is not running。 解决办法是 在调用方法外加一个强制类型转换 str(javaclass.调用方法),转换成python的string类型

wuwaao的专栏 3324

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

antlr4 with python(interface is not good as Java)

antlr4 with python install python config antlr4(need JDK) write you parser syntax and verify it convert to python and install python runtime pip install antlr4-python3-runtime write you customer list...

贾亮的专栏 472

selenium python脚本调用java script 报Message: u'$ is not defined' ; Stacktrace 的解决历程

首先,html如下:js.html 1 <html> 2 <head> 3 <meta http-equiv="content-type" content="text/html;charset=utf-8" /> 4 <title>js</title> 5 <script type="text/ja...

ddzmklhx106577的博客 340

测试面试必备:Python 中的 `!=` 和 `is not` 运算符有什么区别?

在 Python 中,`!=` 和 `is not` 都是用于比较的运算符,但它们的用途和行为有本质区别。本文将详细解释这两个运算符的不同之处,并通过示例和流程图帮助理解。

qq_58299462的博客 833

在java和python和c++中的 比较/null/指针/if/%/字符变数字/round/区别

python is, is not 对比的是两个变量的内存地址 ==,!= 对比的是两个变量的值 java 基本数据类型:值分配在内存栈上 引用数据类型:对象本身的值分配在内存堆上 都是直接分配在内存栈上,不过引用数据类型在栈上分配的是引用,对象本身的存储在内存堆上 比较基本类型 : 没有.equals方法 只能用== 比较对象 object.equals(obj)...

puspos的博客 315

关于appium-python-client报错问题:UnknownError: Original error: JAVA_HOME is not set currently.

关于appium-python-client报错问题:UnknownError: Original error: JAVA_HOME is not set currently.

seabirdssss的博客 542

使用Java 调用 jython 报错 URI is not hierarchical

IllegalArgumentException: URI is not hierarchical] with root cause java.lang.IllegalArgumentException: URI is not hierarchical at java.io.File.<init>(File.java:418) at org.python.core.PrePy.getJarFileNameFromURL(PrePy.java:427) at org.python.core.

人生一张西的博客 1210

Python基础语法:流程控制语句(if、match、for else、while else、try、is、in、not、with、lambda)

Java中的三位运算符是用来实现的。Python中是用来实现的(突然换种语法感觉不太适应)Python 不用?:,而用 x if cond else y,是一个有意的语言设计选择,核心是让代码按自然语序读起来更清楚,降低嵌套时的可读性灾难。Python 确实在“短”与“可读”之间,经常优先后者。为什么不用?

人不风流枉少年 485

frida中使用python调用js脚本报错

通过frida命令绑定js脚本运行正常,但使用python导入frida库调用js版本时报Java is not defined。问题原因是firda-server、frida以及frida-tools之间版本问题。

ding3999的博客 837

python集群到hadoop_如何使用Hadoop流在本地Hadoop集群中运行MRJob?

我正在学习一个大数据类,我的一个项目是在本地建立的Hadoop集群上运行Mapper/Reducer。在我一直在为类使用Python和MRJob库。在下面是我当前用于Mapper/Reducer的Python代码。在from mrjob.job import MRJobfrom mrjob.step import MRStepimport reimport osWORD_RE = re.compi...

weixin_36256917的博客 243
上一篇: Stiff asks, great programmers answer
下一篇: 初识动态语言
mudboy
博客等级 码龄25年 15粉丝 84原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值