• 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 to write Getter/Setter for static variables

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

In the last post, I blogged about how you should write Getter/Setter for member variables.

This is a follow-up for static variables, instead of instance variables.

I didn’t know the answer to that, until I searched around Stackoverflow. There are a couple of ways around using @property on classmethods.

The best answer for me is this:

class MyClass(object):
    _foo = 5
    class __metaclass__(type):
        @property
        def foo(cls):
                return cls._foo
        
        @foo.setter
        def foo(cls, value):
                cls._foo = value

It uses __metaclass__, some kind of black magic in Python.

With that, you can use the getter/setter on the static variable.

>>> MyClass.foo
5
>>> MyClass.foo = 3
>>> MyClass.foo
3
Previous Post

Hide “All My Files” in Mac’s Finder

Next Post

How you should write getter/setter for Python

Next Post
How to write Getter/Setter for static variables

How you should write getter/setter for Python

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