Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4077842
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:36:03+00:00 2026-05-20T17:36:03+00:00

I am from PHP and quick learn OOP Python base PHP ‘s knowledge for

  • 0

I am from PHP and quick learn OOP Python base PHP ‘s knowledge
for example I have a class

<?php
class Cat{
    private $name;
    private $age;
    private $color;

    public function getName()
    {
        return $this->name;
    }

    public function setName($value)
    {
        $this->name = $value;
    }

    public function getAge()
    {
        return $this->age;
    }

    public function setAge($value)
    {
        $this->age = $value;
    }

    public function getColor()
    {
        return $this->color;
    }

    public function setColor($value)
    {
        $this->color = $value;
    }

}
?>

What is equivalence to python OOP code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T17:36:04+00:00Added an answer on May 20, 2026 at 5:36 pm

    An exact equivalent would be this:

    class Cat(object):
        def __init__(self, name...):
            self.__name = name # __ make the var private
            ...
    
        def getName(self):
            return self.__name
    
        def setName(self, name):
            self.__name = name
    
        ...
    

    It will work, but you would never do that in Python. Here is why:

    • Nothing is really private in Python, and you can always bypass restrictions using some tricks in this language. So you usually value more communication, doc and API that enforcing a behaviour.
    • Accessing directly attribute is perfectly fine in Python. If you don’t something special while getting or setting, then you don’t write getters and setters. The best Python libs do this.
    • You have something called ‘properties’ allowing you to change your mind after the fact. If in 1 month, you decide to do something while getting and setting a var, you can do it without changing you API.

    Here is how you would to it in proper Python:

    class Cat(object):
        def __init__(self, name):
            self.name = name
    
    c = Cat('billy')
    print c.name
    # billy
    c.name = 'kit'
    print c.name
    # kit
    

    Now, what if suddenly you want to change your mind and ensure the name is always capitalize on when you store it ? You just add a property:

    class Cat(object):
    
        def __init__(self, name):
            self._name = name # '_' is just a convention and does nothing
    
        @property # when you do cat.name, it will call this function
        def name(self):
            return self._name
    
        @name.setter # when you do cat.name = x, it will call this function
        def name(self, name):
            """ Make the name uppercase when setting it """
            self._name = name.upper()
    

    The code change, but from outside, it looks exactly the same:

    c = Cat('billy')
    print c.name
    # BILLY
    c.name = 'kit'
    print c.name
    # KIT
    

    What are the main benefits?

    • You don’t have to write getter and setter most of the time, it same times and make the code shorter, hence easier to read.
    • If you change your mind, you can always write ONE getter/setter. Only the one you need, not all the other ones. The API keeps looking consistent.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just a quick query really, In my PHP file, I have variables coming from
This example is from php.net: <?php function Test() { static $a = 0; echo
We have an issue using the PEAR libraries on Windows from PHP . Pear
Coming from PHP, I have to do some sql cleanup on this 1000 file
Just a quick question. I want to send data from Javascript to a PHP
Im porting a project from php to java. The project is a web-app based
I want to execute a php-script from php that will use different constants and
I am generating the json from PHP.
I need to perform a HTTP GET from PHP. More specifically, from within /index.php
My attempts to query MySQL from PHP with a create statement of a store

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.