• Home
    • Home – Layout 1
    • Home – Layout 2
    • Home – Layout 3
  • News
  • Technology
  • Gadget
  • Design
No Result
View All Result
  • Home
    • Home – Layout 1
    • Home – Layout 2
    • Home – Layout 3
  • News
  • Technology
  • Gadget
  • Design
No Result
View All Result
Tutorial
No Result
View All Result

How you should write getter/setter for Python

December 9, 2020
in Uncategorized
0 0
0
How to write Getter/Setter for static variables
0
SHARES
2
VIEWS
Share on FacebookShare on Twitter

Coming from the Java world, I am used to writing getter/setter for every member variables that I want to expose. (truth is I made use of IDE to auto generate)

I hate how much code Java has.

With it’s philosophy of readable code, Python is different.

Getter

To write a getter for this member foo:

class MyClass(object):
	
	def __init__(self):
		self._foo = 1

	@property
	def foo(self):
		return self._foo

It uses an annotation @property. Then to access the property:

my_class = MyClass()
print my_class.foo
# 1

Setter

To set a memter, use the annotation @foo.setter (replace foo with your member name).

class MyClass(object):
	...

	@foo.setter
	def foo(self, value):
		self._foo = value
Previous Post

How to write Getter/Setter for static variables

Next Post

Non-ASCII Character in Python File

Next Post
Non-ASCII Character in Python File

Non-ASCII Character in Python File

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Home
  • News
  • Technology
  • Gadget
  • Design

© 2020 AwlNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • Home
    • Home – Layout 1
    • Home – Layout 2
    • Home – Layout 3
  • News
  • Technology
  • Gadget
  • Design

© 2020 AwlNews - Premium WordPress news & magazine theme by Jegtheme.

Welcome Back!

Login to your account below

Forgotten Password?

Create New Account!

Fill the forms below to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In