Reputation: 118
I am coming from Java and learning Python, now. I try to understand the concept of class members in Python.
Here is an example program in Java:
class Hello {
int x = 0;
void ex() {
x = 7;
}
public static void main(String args[]) {
Hello h = new Hello();
System.out.println(h.x);
h.ex();
System.out.println(h.x);
} }
That is what I did in Python, following some examples I found:
class Hello:
def __init__(self) :
self.x = 0
def ex(self):
self.x = 7
h = Hello()
print(h.x)
h.ex()
print(h.x)
Both programs return:
0
7
Here are my questions:
Upvotes: 9
Views: 1855
Reputation: 5226
For what's worth you can save a few line of codes when you have many fields, by using a library to remove the boilerplate. For example with pyfields
:
from pyfields import field, make_init
class Hello:
x = field(default=0)
y = field()
__init__ = make_init()
h = Hello(y=10)
print((h.x, h.y))
yields
(0, 10)
You can even combine it with autoclass
to get several features out of the box:
from autoclass import autoclass
from pyfields import field
@autoclass
class Hello:
x = field(default=0)
y = field()
h = Hello(y=1)
print(h)
assert h == {'x': 0, 'y': 1}
print(dict(h))
yields
Hello(x=0, y=1)
{'x': 0, 'y': 1}
See documentation for details. I'm the author by the way ;)
Upvotes: 0
Reputation: 2974
First, your python code is correct.
It's just a matter about how the languages is designed. Java uses a kind of automatic inference of a reference to the object. It can lead sometimes to strange behaviours for non-java experts:
private int a;
public int add(int a, int b){
return a+b; // what a will it use?
}
So, it's why in java there's the keyword this
that can be used (but you're not forced) in order to solve that ambiguity.
The python team decided to force to use the word self (or any other word but I will explain later) to avoid that kind of problem. You cannot get rid of it. Though, java is still a more verbose language than python and the keyword self doesn't affect a lot that assumption.
However you're not obliged to use the "self" word as a reference to the current object. You can use any other word that would be the first parameter of your method (but it's a very bad practice).
Here, you can see two references that explain deeply why "self is here to stay":
http://www.programiz.com/article/python-self-why
http://neopythonic.blogspot.be/2008/10/why-explicit-self-has-to-stay.html
Upvotes: 7
Reputation: 31
2.)Python is a high level language. where java is more mid-high im guessing. kind of new to java.
python goes by white space, where java requires more formatting.
3.) the self parameter can be removed. replace with whatever you want to call it.
Upvotes: -2
Reputation: 814
So in java the structure is generally
Class
private data (aka the "struct")
public data
constructors (__init__ in python)
functions etc
its very similar in python. same structure just for any functions working with the data you need to put self as an argument. where java it didn't have to take arguments.
Also it seems in python all data is public by default so you don't need to use getters and setters like in java.
personally I find a Python class more of a C-Struct with some added features, where java everything is thrown into a class.
Upvotes: 0