473,408 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Dictionary value is property()

1
I'm trying to subclass a dict object to create an object where some key values = 'property(fget, fset, fdel, doc)'. For some reason when I get the dictionary key a 'property object' is return instead of the result of the 'fget' function.

Expand|Select|Wrap|Line Numbers
  1. class mydict(dict):
  2.  
  3.     def __init__(self, arg):
  4.         self['a'] = arg
  5.         self['b'] = property(self.get_b)
  6.  
  7.     def get_b(self):
  8.         return self['a']
  9.  
  10. d = mydict(5)
  11.  
  12. print d['a'] # Returns 5
  13. print d['b'] # Incorrect. Returns <property obeject>. Should return 5.
  14. print d['b'].fget() # Returns 5. Correct
  15.  
Nov 17 '08 #1
1 2212
bvdet
2,851 Expert Mod 2GB
Function property should work something like this:
Expand|Select|Wrap|Line Numbers
  1. class mydict(dict):
  2.  
  3.     def __init__(self, arg):
  4.         self['a'] = arg
  5.         self.set_b()
  6.  
  7.     def set_b(self):
  8.         self['b'] = self['a']
  9.  
  10.     def get_b(self):
  11.         return self['a']
  12.  
  13.     b = property(set_b, get_b)
  14.  
  15. d = mydict(5)
Expand|Select|Wrap|Line Numbers
  1. >>> d['a']
  2. 5
  3. >>> d['b']
  4. 5
  5. >>> d['a'] = 12
  6. >>> d['b']
  7. 5
  8. >>> d.set_b()
  9. >>> d['b']
  10. 12
  11. >>> d['b']=100
  12. >>> d['b']
  13. 100
  14. >>> 
Calling property is returning a property object and assigning it to key 'b' in your code.
Nov 17 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: john wright | last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I am including the code for the dictionary object. Here is the error I am getting: "System.Exception: Complex...
90
by: Christoph Zwerschke | last post by:
Ok, the answer is easy: For historical reasons - built-in sets exist only since Python 2.4. Anyway, I was thinking about whether it would be possible and desirable to change the old behavior in...
1
by: Martin Widmer | last post by:
Hi Folks. When I iterate through my custom designed collection, I always get the error: "Unable to cast object of type 'System.Collections.DictionaryEntry' to type...
4
by: NullQwerty | last post by:
Hi folks, I have a Dictionary which contains a string key and an object value. I want the object value to point to a property in my class and I want it to be by reference, so that later on I...
20
by: Gustaf | last post by:
This is two questions in one really. First, I wonder how to convert the values in a Dictionary to an array. Here's the dictionary: private Dictionary<Uri, Schemaschemas = new Dictionary<Uri,...
15
by: Dave Young | last post by:
I'm trying to replicate the behaviour of the Commerce.Dictionary object that was found in Commerce Server 3.0 By using a hashtable or creating a custom class that implements IDictionary, you...
2
by: Stephen Costanzo | last post by:
I have: Public Class test Private displayValue As String Private dbType As Integer Property Display() As String Get Return displayValue End Get
6
by: GiJeet | last post by:
hello, I'm trying to use a dictionary as a class member. I want to use a property to get/set the key/value of the dictionary but I'm confused as how to use a dictionary as a property. Since there...
8
by: Andy B | last post by:
I have the object property StockContract.Dictionary which is a dictionary collection of <string, stringkey/value pairs. I need to be able to retreive the keys and their values and display them on a...
1
by: GVDC | last post by:
Example server-side JavaScript Web script, Dictionary class //Dictionary class, hash array unlimited length configurable speed/efficiency // printf("<html><body>"); printf("<b>Creating...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
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,...
0
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...
0
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,...
0
isladogs
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...

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.