Is this how you would define a class "Car" with attribute "Speed" in Python? My background is in Java, and it seems one does not use get/set methods in Python.
class Car(object):
def __init__(self):
self._speed = 100
@property
def speed(self):
return self._speed
@speed.setter
def speed(self, value):
self._speed = value
An accessor method is a function that returns a copy of an internal variable or computed value. A common practice is to name these with the word get. A mutator method is a function that modifies the value of an internal data variable in some way.
Python __str__() This method returns the string representation of the object. This method is called when print() or str() function is invoked on an object. This method must return the String object.
In python you can use getters and setters : And using it is just like using your average attribute syntax. The real reason you need them in Java, is that it's a pain to go refactor everything that uses attribute access when you need to add some extra behaviour.
Accessor methods, also called get methods or getters, allow a way to get the value of each instance variable from outside of the class. In the next lesson, we will see mutator methods, also called set methods or setters, that allow a way to change the values of the instance variables.
In Python we generally avoid getters and setters. Just have a .speed
attribute:
class Car(object):
speed = 0
def __init__(self):
self.speed = 100
See Python is not Java for motivations and more pitfalls to avoid:
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.
Use property
when you have a genuine need to execute code when getting, setting or deleting an attribute. Validation, caching, side effects, etc. all are fair use-cases for properties. Just don't use them until necessary.
Since technically attributes are never private in Python, get/set methods are not considered "pythonic." This is the standard way to access object attributes:
class MyClass():
def __init__(self):
self.my_attr = 3
obj1 = MyClass()
print obj1.my_attr #will print 3
obj1.my_attr = 7
print obj1.my_attr #will print 7
You may, of course, still use getters and setters, and you can somewhat emulate private members by prepending __
to your attributes:
class MyClass():
def __init__(self):
self.__my_attr = 3
def set_my_attr(self,val):
self.__my_attr = val
def get_my_attr(self):
return self.__my_attr
obj1 = MyClass()
print obj1.get_my_attr() #will print 3
obj1.set_my_attr(7)
print obj1.get_my_attr() #will print 7
The __
"mangles" the variable name: from outside some class classname
in which __attr
is defined, __attr
is renamed as _classname__attr
; in the above example, instead of using the getters and setters, we could simply use obj1._MyClass__my_attr
. So __
discourages external use of attributes, but it doesn't prohibit it in the same way that the Java private
modifier does.
There are also, as you mention in your question, properties available in Python. The advantage of properties is that you can use them to implement functions that return or set values that, from outside the class, appear to be simply accessed as normal member attributes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With