Hey!
I'm a hobby programmer since many years now and I've done most of my
latest 'real application' coding in C#. I've played with python of and
on yet not catching on until a few months ago when I got myself hocked
on it for real and now I love C# _and_ Python.
But there is something that keeps bugging me, and that keeps me from
embracing Python even more as a serious alternative to C#(for me). In
C# I make heavy use of Get & Set, mainly to fire an event that some
property has changed so that one can act on that _if one would need
to. I've grown very used to doing it and it feels like the best way to
make very OO and reusuable and easy to use Classes.
Is there _anything_ that I could do in Python which would allow me to
known when a variable has been set or when it's being fetched to allow
me to fire an event there?.
I seriously _hate_ having to use methods for editing variables
(setVariable1(" blah")) since it just adds more code -- or perhaps not
but the code doesn't flow as nicely. So I don't count this as a real
valid option.
This might seem like not such a big thing but to me it's such a
problem that it's driving me to even consider trying out Boo (the
c#/python hybrid) when I get the chance eventhough I think i'd rather
use the _original_ (python) now when i'm almost getting the hang of
it.
How does python coders generally deal with this? Does everyone inherit
from a class and override the methods that sets variables and does the
work from there? I love events and use them heavily -- in GUI apps
what else would one use?
Would be thankful for any pointers that would evolve my pythonic strives. 4 4165
You are in luck because Python has "Properties " just like .NET.
For details lookup the documentation of the built-in function
property(). I'll just paste it here:
property( [fget[, fset[, fdel[, doc]]]])
Return a property attribute for new-style classes (classes that derive
from object).
fget is a function for getting an attribute value, likewise fset is a
function for setting, and fdel a function for del'ing, an attribute.
Typical use is to define a managed attribute x:
class C(object):
def getx(self): return self.__x
def setx(self, value): self.__x = value
def delx(self): del self.__x
x = property(getx, setx, delx, "I'm the 'x' property.")
.... now u can use x like any variable and, python will get & set it
through the appropriate methods. Hope this answers your question.
Thank you!
Wow, this might be exactly what I want! Thanks to the pythonness
(syntax) the code might even be shorter then implementing it in C#!
Gonna go and play around with this some more(now), and can't wait til
I get home (there will be some massive code cleaning).
I wonder why I've never come across this before, feels like i've
googled alot these last weeks.
Python has grown in my eyes.
On 24 Jul 2005 21:06:07 -0700, tharaka <th*********@gm ail.com> wrote: You are in luck because Python has "Properties " just like .NET. For details lookup the documentation of the built-in function property(). I'll just paste it here: property( [fget[, fset[, fdel[, doc]]]]) Return a property attribute for new-style classes (classes that derive from object). fget is a function for getting an attribute value, likewise fset is a function for setting, and fdel a function for del'ing, an attribute. Typical use is to define a managed attribute x: class C(object): def getx(self): return self.__x def setx(self, value): self.__x = value def delx(self): del self.__x x = property(getx, setx, delx, "I'm the 'x' property.") ... now u can use x like any variable and, python will get & set it through the appropriate methods. Hope this answers your question. -- http://mail.python.org/mailman/listinfo/python-list
tharaka wrote: You are in luck because Python has "Properties " just like .NET.
class C(object): def getx(self): return self.__x def setx(self, value): self.__x = value def delx(self): del self.__x x = property(getx, setx, delx, "I'm the 'x' property.")
Just for those that might have tastes like mine; here's the pattern I've
been using for this (in 2.4, obviously):
class C(object):
@apply
def x():
doc = "I'm the 'x' property."
def fget(self): return self.__x
def fset(self, value): self.__x = value
def fdel(self): del self.__x
return property(**loca ls())
You can remove any of fget, fset, fdel, or doc without changing any
other lines, and there are no "extra" entries in the class's name space.
--
season-to-taste-ly yours,
Benji York This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kevin Ly |
last post by:
Consider the test case below. The onmouseout event does NOT fire when
my mouse/cusor is moved off the left side of the browser. It does if
it is moved off the top, bottom, and the right side that HAS THE
SCROLLBAR (does not if the right side does not have the scrollbar on).
Also, if you move around each "this is a test" line, the onmouseout
event fires. How odd? Fortunately, I can have logic to handle this.
The big problem is why...
|
by: Carlo Marchesoni |
last post by:
From an aspx page (A.aspx) I open another one (B.aspx -
for table lookup). When the user selects an entry in
B.aspx I would like to force a button's event in A.aspx
to be fired. I guess the only way is using javascript -
does anybody have a sample for this ?
Thanks
|
by: Keith-Earl |
last post by:
I thought for sure Session End would fire when the user closes his browser,
but no luck. The only way I can get the event to fire is to run a
Session.Abandon, but is that really practicle? When a user is down they
will probably close out the browser or navigate somewhere else.
I want to keep a total count of users in an Application state variable but I
cannot count on the decrement code to run. What can I do to keep up with
Total Users...
|
by: srallen |
last post by:
I have a simple page that has a dropdown with autopostback=true and
onselectedindexchanged is set to a function that never gets called
(checked during debug). This page is subclassed from custom class of
mine that I subclassed from Page.
I have overridden a couple of methods (OnPreRender, OnUnload, OnInit)
to provide for html template layout, all of the base functions are
called as well.
Can anyone lend some help as to why the...
|
by: Verde |
last post by:
This is admittedly an apparently odd request... but please indulge me if you
don't mind:
Suppose I have two <asp:Button.../> on a page (Button1 and Button2). User
clicks Button1 and triggers a PostBack. How can I then fire the click event
of Button2 during the same PostBack?
I know this seems like a totally bad situation I'm creating out of naiveity.
But please indulge. It would save me from having to provide a lengthy
explanation for...
| |
by: OldProgrammer |
last post by:
All the documentation and discussion I have read indicate that the
Session_End is not supposed to fire unless you are in "inProc" Session state
mode, and then only on Session Timeout or at Session Abandon. I need the
event to fire at Session timeout in order to capture and store the Datetime
this occurs. However in the application I am writing, I have been unable
discern any indication that the event is firing. I have used break...
|
by: Ty Salistean |
last post by:
So, here is a wierd question that we have been discussing for a bit now.
Does an event fire even though nothing is subscribed to listen to the event?
For instance, does the Click event of a button fire even though nothing is
subscribed to listen to the event.
The answer is NO in a traditional Publisher/Subscriber design pattern (at
least I think it is).
I have stated that if nothing is subscribed, then the event never gets
|
by: Patrice |
last post by:
Hi all,
Got a strange problem.
In Application_Start, I just store the current date/time in an application
variable to keep track of the last time the application started.
To my surprise, I noticed in my error reporting log (that dumps various
things including this application variable) that sometimes this application
variable is not set.
|
by: John Kotuby |
last post by:
Hi guys,
I am converting a rather complicated database driven Web application from
classic ASP to ASP.NET 2.0 using VB 2005 as the programming language. The
original ASP application works quite well, so at times it is tempting just
to port parts of it over mostly as-is. In fact, one MSDN article I read
suggested using straight HTML wherever possible to make the app more
efficient and less resource demanding.
On one page there are 2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |