Reputation: 5
class teste(object):
def __init__(self,a):
self.a=a
def getA(self):
return self.a
new=teste("lucas")
print new.getA() ######### LINE 1
print new.a ######### LINE 2
Why in classes we always need this "get" methods? i mean, what's the difference between the "LINE 1" and "LINE 2"? they output the same thing.
Upvotes: 0
Views: 514
Reputation: 365945
The other answers already say that you never need getters in Python, and it's just a matter of style.
But the key here is that there is a Pythonic style, and unnecessary getters go against that style. Using unnecessary getters will raise red flags when experienced Python developers read your code. (How many seconds did it take to get three comments saying variations of "Someone used Java too much" here?)
If you read the PEP 8 style guide, under Designing for inheritance, it says:
For simple public data attributes, it is best to expose just the attribute name, without complicated accessor/mutator methods. Keep in mind that Python provides an easy path to future enhancement, should you find that a simple data attribute needs to grow functional behavior. In that case, use properties to hide functional implementation behind simple data attribute access syntax.
PEP 8 is only intended as a style guide for the stdlib, but it's still generally considered a good description of what idiomatic Python looks like ("TOOWTDI" means any descriptive guide is implicitly a prescriptive guide, at least in theory), and there are nice testers and fixers that let you test your own code against PEP 8 guidelines.
While I'm at it, William's answer linked to why use getters and setters, a language-agnostic question, where the accepted answer is a list of reasons why accessors are sometimes a good idea. Almost none of them are relevant to Python. Most can be answered with one word: @property
(or, in the last case, attrgetter
). For the rest:
mock
, pickle
, etc. are designed to operate with attributes.Upvotes: 1
Reputation: 3034
You don't in Python.
Note that getters and setters are frequently considered "unpythonic" (related: python -c 'import this'
).
Getters are just a style. Sometimes you might want to use a getter but it is not always necessary. See why use getters and setters for a sort of general discussion on getters and setters.
For more relevancy to Python, consider:
An opinion piece,
another opinion piece, some sample code or some other sample code.
Upvotes: 4
Reputation: 304375
Normally in Python we just use normal attribute access. If you decide you need "getters" and "setters" later on, you can easily turn those into properties. You don't need to clutter up your code "just in case"
One imporant point to me is that you don't need to change any code that uses the class when you do this.
Upvotes: 0
Reputation: 208565
You don't need to use "get" methods, the only difference in your example is that if you use new.a
you can assign it a new value.
So if you intend to have a read only attribute then a get method is appropriate. (note that if the method returns a mutable object, you can modify that object, so not technically read only)
Upvotes: 0
Reputation: 4113
In python you don't need get methods. It is a matter of coding style.
Upvotes: 3