python global getter and global setter functions

328 views
Skip to first unread message

shang

unread,
Feb 15, 2010, 6:15:57 AM2/15/10
to Utah Python User Group
hi all,
I'm new to python but I know a little php here is a code in php that
make global setter and global getter for both static properties and
dynamic properties my question is :
is python powerfull enough to make the same feature using object
oriented in it if the answer is yes can anybody give me a sample of
code the do the same thing as this full working example????????????

thanks in advance

here is the code
<?php
$line="<br />\n";
class Person
{ // class person start
var $name, $age;
static $salary;
function __construct($name, $age,$salary)
{ //construct start
$this->set( "name",$name);
$this->set( "age",$age);
self::sset("salary",$salary);
} // construct end

function set($property,$value)
{// setter start
$this->$property=$value;
}//setter end

function get($property)
{//getter start
return $this->$property;
}//getter end

function sset($property, $value)
{//static setter start
self::$$property=$value;
}// static setter end

function sget($property)
{//static getter start
return self::$$property;
}//static getter end

}// class person end

$x=new Person("hatem",35,10000);

$y=new Person("atif",40,12000);

// printing x values
echo "x.name: ".$x->get("name").$line;
echo "x.age: ".$x->get("age").$line;
echo "x.salary: ".$x->sget("salary").$line;

//printing y values
echo "y.name: ".$y->get("name").$line;
echo "y.age: ".$y->get("age").$line;
echo "y.salary: ".$y->sget("salary").$line;
?>

Shawn Willden

unread,
Feb 15, 2010, 8:19:12 AM2/15/10
to utahpython
On Mon, Feb 15, 2010 at 4:15 AM, shang <shan...@gmail.com> wrote:
> hi all,
> I'm new to python but I know a little php  here is a code in php that
> make global setter and global getter for both static properties and
> dynamic properties  my question is :
> is python powerfull enough to make the same feature

Yes, although I think in 99.9% of cases it's a bad idea.

Look into the __dict__ attribute. Each instance object has one, and
it's a dictionary of all of the user-defined attributes. For example:

class Person(object):
def __init__(self, name):
self.name = name

def report(self):
print "name: " + self.__dict__["name"]

You could write little get() and set() functions that just manipulate
the __dict__, but there's really no reason to. For example:

def set(self, attr, value):
self.__dict__[attr] = value

and then you could write:

x = Person("Shawn")
x.set("name", "Hatem")

but that's the same as writing:

x.__dict__["name"] = "Hatem"

or even just:

x.name = "Hatem"

You might want to look into not using a class at all if you need to do
a lot of this sort of "dynamic" instance access. Just use a
dictionary and write some functions to manipulate your dictionaries.
You can use modules for scoping.

--
Shawn

Hatem El-Zanaty

unread,
Feb 15, 2010, 8:34:14 AM2/15/10
to utahp...@googlegroups.com
thanks man


--
You received this message because you are subscribed to the Google Groups "Utah Python User Group" group.
To post to this group, send email to utahp...@googlegroups.com.
To unsubscribe from this group, send email to utahpython+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/utahpython?hl=en.


Christian Horne

unread,
Feb 15, 2010, 12:31:38 PM2/15/10
to utahp...@googlegroups.com
i'm gonna reply to this too, *don't use getters and setters in python EVER*.

>> utahpython+...@googlegroups.com<utahpython%2Bunsu...@googlegroups.com>


>> .
>> For more options, visit this group at
>> http://groups.google.com/group/utahpython?hl=en.
>>
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Utah Python User Group" group.
> To post to this group, send email to utahp...@googlegroups.com.
> To unsubscribe from this group, send email to
> utahpython+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/utahpython?hl=en.
>
>


--
the blendmaster

Hatem El-Zanaty

unread,
Feb 16, 2010, 3:17:19 AM2/16/10
to utahp...@googlegroups.com
thanks man
Reply all
Reply to author
Forward
0 new messages