First of all, i come from a java and php background and am now learning python. This may therefore seem like a silly question but it has me a little confused.
As an example in php:
class MyClass
{
private $myMember;
public function __construct($myMember = null)
{
$this->myMember = $myMember;
}
public function setMyMember($myMember)
{
$this->myMember = $myMember;
}
public function getMyMember()
{
return $this->myMember;
}
}
This is completely textbook 101 OOP. A class with a single private member and a getter and setter.
However, learning some python and this isn’t so straightforward it seems.
First off, the constructor isn’t a real constructor (but you can use at such?? )
Secondly, any class level decelerations are treated as static ?? So self.myMember in python actually sets a different variable on the instance which is not the same as the class level myMember?
Lastly, if my confusions are correct - how would the same example above be coded in python?
Generally, you don't use getters and setters in Python; unless you need to transform the value, need side-effects during setting or getting, or need to prevent either getting or setting, you just use attributes instead:
class MyClass(object):
def __init__(self, member):
self.member = member
Instance attributes are separate from class attributes; setting attributes on self means you are manipulating the instance, attributes on the class (MyClass.member) are class attributes and are thus shared amongst instances. This is achieved by using the class as a fallback when looking up attributes; first the instance is queried, then the class of the instance, then the base classes of the class; setting self.member means that any MyClass.member is no longer visible on the instance.
The __init__ method is indeed, technically, not a constructor. self already exists when __init__ is called. It is instead an initializer, you set the first values with it. If you need an actual constructor (instance factory) you need to look for the __new__ static method instead. Note that in PHP, __construct is an initializer as well, not a constructor!
For those cases where you do have a need to create setters and getters, use the property decorator function:
class MyClass(object):
def __init__(self, member):
self._member = member
@property
def member(self):
return 'Hello {}!'.format(self._member)
@member.setter
def member(self, value):
# Remove "Hello " and "!" from the start and end of any value being set.
if value.startswith('Hello '):
value = value.split(None, 1)[1]
self._member = value.rstrip('!')
Demo:
>>> m = MyClass('World')
>>> m.member
'Hello World!'
>>> m.member = 'Hello Planet Earth!'
>>> m.member
'Hello Planet Earth!'
>>> m._member
'Planet Earth'
This would be something like this:
class MyClass(object):
def __init__(self, myMember=None):
self._myMember = myMember
@property
def myMember(self):
return self._myMember
@myMember.setter
def myMember(self, value):
self._myMember = value
Demo:
c = MyClass()
c.myMember = 10
print c.myMember
And you have:
# 10
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