Jump to content

Do you use get/set method in python

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
solartic

solartic

    Learning Programmer

  • Members
  • PipPipPip
  • 95 posts
Hi guys

Been trying to get serious with python over the last few days and Ive have not seen a lot of get and set methods used in most of the code example.

So my question is do you find get and set methods all that useful in python and not necessarily methods prefixed with get but just using methods to access and manipulate class variables instead of directly.

I know this is more a matter of personal preference and productive code can be achieved with or without these methods I'm just trying to see what the general python practices are

#2
ferovac

ferovac

    Learning Programmer

  • Members
  • PipPipPip
  • 84 posts
well if you are programming a quick script that will do some quick job for you then you will probably not use get and set, or classes for that matter, but if you are going to be in a programming team, and be in charge of a piece of a application, then yes of course you will use set , get and classes

actually it all depends on the buyers demands (very rarely will that happen ), but generally you will be using that, as in all the languages that are OO ( object oriented )

#3
solartic

solartic

    Learning Programmer

  • Members
  • PipPipPip
  • 95 posts
Thanks for your input ferovac, very helpfull

#4
manux

manux

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 234 posts
It isn't overly used in Python, but there's something extremely powerful in Python which renders the "direct" access(C.foo instead of C.getFoo()) of member variables to be "secure" in a way.
They're called properties, when you write C.foo, it won't just return C.foo, it'll call a function you choose, like C.getFoo(), but transparently.
Here's a nice example: The Python Property Builtin

Then again, as in any OO language, it's a good practice.
But in Python, I think, things like properties are better because it allows things to be done in the background, so that the end-user is not annoyed.

As for usage and general Python practices, I think most people don't use it because it's often not essential and Python code is often meant to be written rapidly rather than thoroughly.

#5
solartic

solartic

    Learning Programmer

  • Members
  • PipPipPip
  • 95 posts
Thanks manux, I do agree its good practice but sometime they just seem to be more of an annoyance that anything. I actually tried property() over the weekend and I like it a lot, it make it easier to access class variables for me personally and you still have your method ready and waiting if you ever feel like switching. oh and thank for the link