One on One with Python
People say Python is very simple yet very complex. Let's find out what Python has to say about herself.
Q. How would you define yourself in simple terms?
Ans. I am a general purpose and high level programming language. For geeks, I would define myself as interpreted, object-oriented programming language. They all like these big terms :) .
Q. What makes you so unique?
Ans. I abstract many sophisticated details from the programming code. My main focus is on abstraction so that it can be understood by most novice programmers. For example, if you have to write a "Hello World" program using my "friend" JAVA you have to write:
But if you have to write using me, it would be something like:
Q. Ohh this looks pretty straight forward to me. Can you shed some light on how easy it is to do a setup?
Ans. Aaah that's super easy too. Minimal setup is something I strive for. If you’re on a Mac, just open the Terminal program, type “python”, and press enter. If I m there, you’ll see the version and then my interpreter will start. Then you can easily write your first Python statement: print(‘Hello World!’). But if its not installed, it’s super easy to download and install.
My aim is instead of focusing on how to get your code to even run, you’ll be able to focus on learning actual programming concepts.
Q. Why companies like Google, Dropbox, Spotify, and Netflix love you so much?
Ans. If you take a look at these companies, you can see they love me for ease of use and because I m great in rapid prototyping and iteration. You can also see that I can be used for a wide variety of applications, and as you learn the basics of me, you’ll be able to create almost anything you want. Many great developers contribute daily in my community by creating Python libraries. These libraries can help you get started so that you don’t have to write code to reinvent the wheel. So for example, if you want to do complex image processing, the Python Imaging Library will help you get started. Want to create games? PyGame is a Python game engine. If data science is your thing, SciPy is the library for you.
Q. The most famous question ... Are you more powerful than C++?
Ans. See, I don't think there is one answer for this. It really depends on what you mean by “powerful.” Access the power of the hardware? Then C/C++ is your answer. Do powerful things more easily? Then Python is your answer.
Q. I really want to dig deeper in Java vs Python. So, how object oriented programming is different in Java and you?
Ans. To begin, lets implement the same small class in both Python and Java to illustrate the differences between them.
First, assume you have the following Car class definition in Java:
Java classes are defined in files with the same name as the class. So, you have to save this class in a file named Car.java. Only one class can be defined in each file.
A similar small Car class is written in Python as follows:
In Python you can declare a class anywhere, in any file, at any time. Save this class in the file car.py.
Q. How you manage class and object attributes? How is it different from Java?
Ans. It's an interesting question. In Java, you declare attributes in the class body, outside of any methods, with a definite type. You must define class attributes before they are used:
On the other hand, if you use me, you declare and define attributes inside the class __init__(), which is the equivalent of Java’s constructor:
By prefixing the variable names with self, you tell me these are attributes. Each instance of the class will have a copy. All variables in me are loosely typed, and these attributes are no exception.
Q. As we are talking so much on Java, one more question. Java controls access to methods and attributes by differentiating between public data and private data. Do you have same public and private data?
Ans. I don't have the same notion of private or protected data that Java does. Everything in me is public.
Instead of private, I have a notion of a non-public instance variable. Any variable which starts with an underscore character is defined to be non-public. This naming convention makes it harder to access a variable, but it’s only a naming convention, and you can still access the variable directly.
I should highlight one point though, I let you access ._cupholders, but IDEs such as VS Code may issue a warning through linters that support PEP 8.
Q. Can you please shed some light on how you support inheritance?
Ans. I do support multiple inheritance, or creating classes that inherit behavior from more than one parent class.
To see how this works, update the Car class by breaking it into two categories, one for vehicles, and one for devices that use electricity:
A Vehicle is defined as having .color and .model attributes. Then, a Device is defined to have a ._voltage attribute. Since the original Car object had these three attributes, it can be redefined to inherit both the Vehicle and Device classes. The color, model, and _voltage attributes will be part of the new Car class.
In the .__init__() for Car, you call the .__init__() methods for both of the parent classes to make sure everything is initialized properly. Once done, you can add any other functionality you want to your Car. In this case, you add a .year attribute specific to Carobjects, and getter and setter methods for .voltage.
Functionally, the new Car class behaves as it always has. You create and use Car objects just as before:
Q. How do you do variable typing?
Ans. I use a concept called duck typing, which in basic terms means that if a variable “walks like a duck and quacks like a duck, then it’s a duck.” Instead of identifying objects by type, I simply examines their behavior.
Q. I have heard a lot about dunder methods. What are those?
Ans. As it quickly became tiresome to say under-under-method-under-under Pythonistas adopted the term “dunder methods”, a short form of “double under.” They special methods which are a set of predefined methods you can use to enrich your classes. They are easy to recognize because they start and end with double underscores, for example __init__ or __str__.
Dunder methods let you emulate the behavior of built-in types. For example, to get the length of a string you can call len('string'). But an empty class definition doesn’t support this behavior out of the box:
To fix this, you can add a __len__ dunder method to your class:
Q. What is your message to all Python developers?
Ans. Few thing I want to say:
"Explicit is better than implicit"
"Readability counts."
"Anybody can fix anything."
Fix each broken window (bad design, wrong decision, or poor code) as soon as it is discovered.
"Now is better than never."
Test ruthlessly. Write docs for new features.
Even more important that Test-Driven Development--Human-Driven Development
Q. In the end, can you talk a little bit on your “Value-system"?
Ans. I find these very valuable, of-course in no particular order:
- "Build tools for others that you want to be built for you."
- "Simplicity is alway better than functionality."
- "Fit the 90% use-case. Ignore the nay sayers." -
- "Beautiful is better than ugly."
- Build for open source (even for closed source projects).
Thanks a lot Ms. Python for such a useful session.
Mapping Product Owner
5 年Wow??
Principal Software Engineer at HERE Technologies
5 年This is very cool!!
Group Software Development Manager@Intuit | Ex-HERE | #GHC2024 /2020 Grace Hopper Speaker
5 年Awesome read Isha!