473,554 Members | 2,324 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about properties.

Hi,

i read in a book the following code snippet that is dealing with
properties:

class ProtectAndHideX (object):
def __init__(self, x):
assert isinstance(x, int), '"x" must be an integer!"'
self.__x = ~x

def get_x(self):
return ~self.__x

x = property(get_x)
Can anyone please help me understand what the symbol "~" does here ??

Thanks for any help!

Aug 10 '07 #1
8 975
On Aug 10, 12:21 pm, king kikapu <aboudou...@pan afonet.grwrote:
Hi,

i read in a book the following code snippet that is dealing with
properties:

class ProtectAndHideX (object):
def __init__(self, x):
assert isinstance(x, int), '"x" must be an integer!"'
self.__x = ~x

def get_x(self):
return ~self.__x

x = property(get_x)

Can anyone please help me understand what the symbol "~" does here ??

Thanks for any help!
>>help(2)
.....
| __invert__(...)
| x.__invert__() <==~x

hth.
Duikboot
Aug 10 '07 #2
On Fri, 10 Aug 2007 03:21:29 -0700, king kikapu wrote:
Hi,

i read in a book the following code snippet that is dealing with
properties:

class ProtectAndHideX (object):
def __init__(self, x):
assert isinstance(x, int), '"x" must be an integer!"'
self.__x = ~x

def get_x(self):
return ~self.__x

x = property(get_x)
Can anyone please help me understand what the symbol "~" does here ??
This has nothing to do with properties. For integer objects ``~`` is the
bitwise negation or invertion operator.

Ciao,
Marc 'BlackJack' Rintsch
Aug 10 '07 #3
On Aug 10, 1:33 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
On Fri, 10 Aug 2007 03:21:29 -0700, king kikapu wrote:
Hi,
i read in a book the following code snippet that is dealing with
properties:
class ProtectAndHideX (object):
def __init__(self, x):
assert isinstance(x, int), '"x" must be an integer!"'
self.__x = ~x
def get_x(self):
return ~self.__x
x = property(get_x)
Can anyone please help me understand what the symbol "~" does here ??

This has nothing to do with properties. For integer objects ``~`` is the
bitwise negation or invertion operator.

Ciao,
Marc 'BlackJack' Rintsch
Xmmm...ok then but what is actually doing there ?? I removed it and
things seems to work the same way...

Aug 10 '07 #4
Maybe is just a writers' "play" and nothing else.

Aug 10 '07 #5
king kikapu wrote:
On Aug 10, 1:33 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
>On Fri, 10 Aug 2007 03:21:29 -0700, king kikapu wrote:
>>Hi,
i read in a book the following code snippet that is dealing with
properties:
class ProtectAndHideX (object):
def __init__(self, x):
assert isinstance(x, int), '"x" must be an integer!"'
self.__x = ~x
def get_x(self):
return ~self.__x
x = property(get_x)
Can anyone please help me understand what the symbol "~" does here ??
This has nothing to do with properties. For integer objects ``~`` is the
bitwise negation or invertion operator.

Ciao,
Marc 'BlackJack' Rintsch

Xmmm...ok then but what is actually doing there ?? I removed it and
things seems to work the same way...
Observe the name of the class. I believe the integer value is inverted
merely as a demonstration that the value can be "obscured" somehow - in
a more complex example the author might have insisted in string values,
the encrypted them. It's not essential to the example, it merely shows
that the value retrieved from the property can be computed from
underlying attributes.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Aug 10 '07 #6
Hi,

i read in a book the following code snippet that is dealing with
properties:

class ProtectAndHideX (object):
def __init__(self, x):
assert isinstance(x, int), '"x" must be an integer!"'
self.__x = ~x

def get_x(self):
return ~self.__x

x = property(get_x)
Can anyone please help me understand what the symbol "~" does here ??
My guess is that the example tries to show that it does not matter how the
property computes the value. You can -- if you want -- to store integers
as their bit-inverted versions (the ~ operator) and then do the conversion
when getting the property value.

Assume you initialized the object with ProtectAndHideX (4). Outside the
object you don't have access to the original __x. And! Even if you changed
the name of the variable name to y, you'd have hidden_x.y == -5 instead of
4.

The example is very contrived. There might be some security related cases
where you need to hide what you store in memory, though. (Hopefully they
do more than just invert the bits! :)

NB: I don't know what the original author was thinking here -- my
telepathy isn't what it used to be.

--
[ Antti Rasinen <*ar*@iki.fi ]

Aug 10 '07 #7
On Aug 10, 5:31 am, dijkstra.ar...@ gmail.com wrote:
On Aug 10, 12:21 pm, king kikapu <aboudou...@pan afonet.grwrote:
Hi,
i read in a book the following code snippet that is dealing with
properties:
class ProtectAndHideX (object):
def __init__(self, x):
assert isinstance(x, int), '"x" must be an integer!"'
self.__x = ~x
def get_x(self):
return ~self.__x
x = property(get_x)
Can anyone please help me understand what the symbol "~" does here ??
Thanks for any help!
>help(2)

....
| __invert__(...)
| x.__invert__() <==~x

hth.
Duikboot
http://docs.python.org/ref/unary.html

The unary ~ (invert) operator yields the bit-wise inversion of its
plain or long integer argument. The bit-wise inversion of x is defined
as -(x+1). It only applies to integral numbers.

Aug 10 '07 #8
king kikapu wrote:
>On Aug 10, 1:33 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:

>>On Fri, 10 Aug 2007 03:21:29 -0700, king kikapu wrote:

>>>Hi,
i read in a book the following code snippet that is dealing with
properties :
class ProtectAndHideX (object):
def __init__(self, x):
assert isinstance(x, int), '"x" must be an integer!"'
self.__x = ~x
def get_x(self):
return ~self.__x
x = property(get_x)
Can anyone please help me understand what the symbol "~" does here ??

This has nothing to do with properties. For integer objects ``~`` is the
bitwise negation or invertion operator.

Ciao,
Marc 'BlackJack' Rintsch


Xmmm...ok then but what is actually doing there ?? I removed it and
things seems to work the same way...
I guess it is the `Hide' part of the Protectand*Hide * class.
Gerardo
Aug 10 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1936
by: lawrence | last post by:
Suppose I have this table: CREATE TABLE properties ( id int(11) NOT NULL auto_increment, name varchar(255) NOT NULL default '', modifierText text NOT NULL, modifierChar char(1) NOT NULL default '', modifierVarchar varchar(255) NOT NULL default '', modifierInt int(11) NOT NULL default '0',
21
4042
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class A { std::vector<B*> Bs; public:
6
2821
by: amethyste | last post by:
hello, This is my sample (simplified): public class Binom { public string _Value;
15
1847
by: designconcepts | last post by:
bo'jour, bo'jour, So I have question to present to the forum about OOD. This is a Csharp forum, but C# is the lang of choice and the question is an exercise based on some comments by the chief designer of C#. Those of you who are junkies for design principle might be interested in contributing to this thread. I was recently reading some...
12
1146
by: einar | last post by:
I have aa situation and i don't know if what i want to do is possible. I have a class named Account with some properties and i set values for these properties like this. public void foo() { account.BillingCity = "Wichita"; account.BillingCountry = "US"; account.BillingState = "KA";
14
3115
by: 42 | last post by:
Hi, Stupid question: I keep bumping into the desire to create classes and properties with the same name and the current favored naming conventions aren't automatically differentiating them... (both are "Pascal Case" with no leading or trailing qualifiers). For example... I'll be modelling something, e.g. a computer, and I'll
14
1818
by: Mr Newbie | last post by:
I am often in the situation where I want to act on the result of a function, but a simple boolean is not enough. For example, I may have a function called isAuthorised ( User, Action ) as ????? OK, this function may return a boolean, and if this is true, then no message back is really required, but if it fails then some supporting message...
0
327
by: AdamKadmon | last post by:
Hello Group, Below is (mostly)functional code that enumerates certain properties for all of the groups in my Active Directory. My question is contained within comments as well as here: Once I have assigned the value of Properties to a variable and spit that value out to the console (ideally to a Stored Proc) and the foreach loops again,...
6
1697
by: RSH | last post by:
I am still trying to grasp the use of real world Objects and how to conceptualize them using a business scenerio. What I have below is an outline that I am wrestling with trying to figure out a class structure:\ Top level Objects: Companies Employees
11
2841
by: manstey | last post by:
Hi, I am having trouble designing my classes. I have two classes. The first one wraps around an old-style class called oref Class CacheClass(object): def __init__(self, obj):
0
7536
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...
1
7576
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7895
tracyyun
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...
0
6163
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5165
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3583
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...
0
3564
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1147
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
853
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.