• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

Python learnings

 
Ranch Foreman
Posts: 2390
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been programming in Java for several years. Recently since our project involved Python work I have been learning it.Below are some of my learnings :

Python is easy to get started with quickly.It is a scripting language, procedural as well as object oriented.It is interpreted language and not compiled one.

In Python programs, there is no mandatory entry point and it starts running from the top.Optionally we can provide a main function to be called when running as a script (instead of importing).

In python we don't need the formality of  writing var type for variables.For example we can write



For which in java we would write var or int or String etc before the variable.

In python we don't need to put semicolon at the end of statement.

In python we don't need braces for functions, blocks etc .All we have to do is maintain an identation.

We can return more than one value from a method.

Any other essential things which I should learn next ?Thanks
 
Monica Shiralkar
Ranch Foreman
Posts: 2390
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I find it surprising that Python has same naming convention for function names as that of variables, that is words separated by underscores. Having different one would have made code more readable.
 
Saloon Keeper
Posts: 23282
158
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are several different name-forming schemes, but most are based either on lower-case+underscore (C-like) or camelCase (Java-like), with some variations. Actually, I'm bad about mixing the two in Python. In general, when I'm using it in OOP form, I go for camelCase and when I'm doing procedural stuff, I go c_like, but don't expect consistency from me.

In Fortran, you didn't have case-sensitive names, Or underscores. I miss that sometimes.
 
Ranch Foreman
Posts: 235
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java, treating non-booleans as booleans is considered so bad that it won't even compile.

In Python, various objects have a "truthiness" about them that varies with their contents, and directly making use of this is a big part of normal idiomatic Python coding.

Some of them would be pretty surprising.

In general, because the list, tuple and dict are 'baked into' Python, and have language support rather than a library we all tend to have, there are explicit syntaxes for passing tuples, dicts, etc. in function signatures that are very different than Python...

Some of these lines should be edited, but here's what I did when I was playing "Truth or Dare" with the Python interpreter:

 
Jesse Silverman
Ranch Foreman
Posts: 235
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The calling conventions for functions are very flexible in Python and extremely different from the static OOPS languages (and C for that matter).

I still have culture-shock due to coming from a very uptight world where we were extremely fussy about knowing EXACTLY how we could be called, details of function overloading rules, etc.

Python has both positional parameters and named parameters which they insist on calling keyword arguments, also you can roll and unroll things into tuples or dicts right in the method definitions and calls.  This is all VERY different from Java, C/C++, C# etc.:

https://docs.python.org/3.9/tutorial/controlflow.html#more-on-defining-functions

Come to think of it, Java doesn't even support default parameters.  Python does but there are some gotchas that are specific to Python, I think also explained well on that page.

 
Jesse Silverman
Ranch Foreman
Posts: 235
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another thing you may "know" from Java where Python decided to behave differently.

In Python, division by zero results in an exception, ALWAYS, even when both are float type.

In Java, you can get +Infinity or -Infinity or NaN when doing division with floats or doubles or a mix of double or float and int, as per IEEE 754.

Not in Python.

The % operator also works differently than you are used to when the numbers are of different signs, giving different answers than Java does.
This is on purpose not by accident.

People sloppily describe "floor division" as dropping the fractional part, but it rounds towards -Infinity when you have different signs...

-5 // 2

>>> -5 // 2
-3
>>> 5 // -2
-3

So there are a few places where if you just picked up your Java code and dropped it into Python or vice versa, you would get some pretty unexpected behavior on some pretty basic operations.

I focused on these which I was just thinking about because for the most part knowing many languages helps you in all of them, except for the cases where one of them decides to have "better" or "more sensible" behavior.  The things I showed are definitely not errors, they were done on purpose, but are traps for people hopping back and forth from one language to another...
 
Monica Shiralkar
Ranch Foreman
Posts: 2390
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:There are several different name-forming schemes,



But isn't it good to follow the 1 naming scheme that is the one which
python website tells.


https://www.python.org/dev/peps/pep-0008/
 
Jesse Silverman
Ranch Foreman
Posts: 235
8
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Monica:

I'm working (yesterday and today) on getting Python's OOPS conventions to stop seeming weird to me.

As I think you spend more time in Java than anything else, this particular article might make sense to you:
https://realpython.com/oop-in-python-vs-java/

I have been reading other things first, however, none of them expected Java to be "what seemed normal", which that article does.

I feel people who did lots of C# will have a little less culture shock than Java-heads.
I look at the class definition and think "Where are the instance variables??  What are their types?"
It's all there in the conveniently named __init()__

Jesse
 
Marshal
Posts: 72088
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That looks a nice website, JS

I look at the class definition and think "Where are the instance variables??  What are their types?"

You cannot guarantee that the class' designer wants all the fields initialised from constructor arguments. Maybe some of the fields have sensible default initial values.
 
Jesse Silverman
Ranch Foreman
Posts: 235
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm just learning here, but my initial take away (see what I did there?) was that those sensible defaults go into the constructor as well in Python.

What you don't see is the list of declared types and attribute names free-standing anywhere like C/C++/Java/C#

Still getting used to it, but that was my initial impression.
 
Jesse Silverman
Ranch Foreman
Posts: 235
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got my first correction in the Official Python tutorial today.

One about sequencing of presentation was closed due to "it was just me grousing" (it explains function-calling styles referencing dicts and tuples which haven't been introduced yet), two others are still open and haven't been closed or addressed by a change request, but hey, the official Python docs now are a teeny bit different because of my annoyingly detail-oriented nature.

https://bugs.python.org/issue43042

I wasn't joking, I was really staring at it thinking "Does it not do that for recursive calls also?"

I think the point here is that Python is definitely going to be a real third or fourth language for me in my life.
 
Tim Holloway
Saloon Keeper
Posts: 23282
158
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That looks a nice website, JS

I look at the class definition and think "Where are the instance variables??  What are their types?"

You cannot guarantee that the class' designer wants all the fields initialised from constructor arguments. Maybe some of the fields have sensible default initial values.



The horrible thing about OO Python is that a Python class is a dictionary. Meaning that you can create instance variables within any member function without having named them explicitly in the class definition.

There is no concept of "public" and "private" members, so the distinction is made via naming conventions (starts with underscore).

And of course, it's one of those languages where "constants - aren't and variables - won't". No manifest constants.

Needless to say, these features mean that large-scale Python code can get quite fragile, which is why some of the major players have been forced to invent special tools to attempt to foil some of the issues that can arise.
 
Jesse Silverman
Ranch Foreman
Posts: 235
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:

The horrible thing about OO Python is that a Python class is a dictionary. Meaning that you can create instance variables within any member function without having named them explicitly in the class definition.

There is no concept of "public" and "private" members, so the distinction is made via naming conventions (starts with underscore).

And of course, it's one of those languages where "constants - aren't and variables - won't". No manifest constants.

Needless to say, these features mean that large-scale Python code can get quite fragile, which is why some of the major players have been forced to invent special tools to attempt to foil some of the issues that can arise.



Isn't it much worse than you make it out to be, tho?
Not that I am grousing, I am trying to understand.

From what I understand, the default levels of protection despite _convention and __conventions, is that code OUTSIDE OF YOUR CLASS can create instance variables, you know, like, WHEREVER DUDE, and, for that matter delete them!  Maybe change a float to a list of strings for fun?

I have a good friend who is a good person, who somehow maintains the belief that in his favorite languages you can change the value of numeric (and other) constants is somehow a good thing, denoting 'flexibility'.  He finds Java, C++, C# etc. "stiff and uptight"...

I think it is the page I linked to, or something else from them one link away, that includes a reference to Guido Von Rossum saying "Hey, we are all consenting adults here."

Don't get me wrong, I love Python, but working on code that controls people's life savings, for instance, I feel like I might get slapped with a paternity suit if I'm not careful, despite being a consenting adult.

I am not sure how much the best linters protect you from these things, but my initial feeling is "kinda a lot", hence my interest therein.
 
Monica Shiralkar
Ranch Foreman
Posts: 2390
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:

As I think you spend more time in Java than anything else, this particular article might make sense to you:
https://realpython.com/oop-in-python-vs-java


Thanks
 
Monica Shiralkar
Ranch Foreman
Posts: 2390
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote: but working on code that controls people's life savings, for instance, I feel like I might get slapped with a paternity suit if I'm not careful, despite being a consenting adult.
.



What does this mean ?
 
Jesse Silverman
Ranch Foreman
Posts: 235
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Python lets you do a lot of stuff that would never ever compile in Java/C#/C++.
Some of it could easily do Very Bad Things that would have economic consequences, if you miss just a test or two.
That scares me a bit.

Of course, causing you to have children is NOT one of those likely consequences.
That specific consequence I feared was just a joke because the creator of Python dismissed these sorts of protections by stating we are "Consenting Adults".
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic