Questions tagged [descriptor]

490 questions
102
votes
7 answers

How to call a property of the base class if this property is being overwritten in the derived class?

I'm changing some classes of mine from an extensive use of getters and setters to a more pythonic use of properties. But now I'm stuck because some of my previous getters or setters would call the corresponding method of the base class, and then…
UncleZeiv
  • 18,272
  • 7
  • 49
  • 77
31
votes
4 answers

Python dict.get('key') versus dict['key']

Why does this throw a KeyError: d = dict() d['xyz'] But this does not? d = dict() d.get('xyz') I'm also curious if descriptors play a role here.
foundling
  • 1,695
  • 1
  • 16
  • 22
29
votes
2 answers

Subscribe to a BLE Gatt notification Android

I´m developing an BLE app, based on the Gatt sample project provided by google: https://developer.android.com/samples/BluetoothLeGatt/index.html. So, I can send data writing in a characteristic successfully. Now I need to know when this…
Orlando
  • 467
  • 1
  • 7
  • 11
29
votes
3 answers

Descriptors as instance attributes in python

To the question: Why can't descriptors be instance attributes? it has been answered that: descriptor objects needs to live in the class, not in the instance because that is the way that the __getattribute__ is implemented. A simple example.…
Hernan
  • 5,811
  • 10
  • 51
  • 86
28
votes
7 answers

Eclipse 'loading descriptor' takes ages

We have a Java Spring MVC based project using Eclipse (Juno - the latest build), using the latest JVM 1.7 and Tomcat 7. Eclipse is pretty fast, and everything is set to default settings. Once it is all loaded up, it is lightning fast, which makes a…
Toby
  • 1,651
  • 2
  • 18
  • 31
26
votes
3 answers

descriptor '__init__' of 'super' object needs argument

I'm trying my hand at making an Object-Oriented text-based game in Python, and attempting to implement my first properties and decorators. Using the chapter 5 in the book 'Python 3 Object Oriented Programming', I've tried to use the examples and…
Automatic Bazooty
  • 502
  • 2
  • 6
  • 13
25
votes
5 answers

python, __slots__, and "attribute is read-only"

I want to create an object in python that has a few attributes and I want to protect myself from accidentally using the wrong attribute name. The code is as follows: class MyClass( object ) : m = None # my attribute __slots__ = ( "m" ) #…
grigoryvp
  • 40,413
  • 64
  • 174
  • 277
25
votes
2 answers

Python descriptor vs property

I'm confused as to when to use a property vs a descriptor. I read that a property is a specialized descriptor. Can someone please post how this works?
ZevGriner
  • 409
  • 1
  • 4
  • 6
19
votes
3 answers

What is the difference between "descriptor" and "signature"?

I am now using ASM (Java bytecode instrumentation library). To retrieve the signature of given method, there is a field which is named "desc". I guess this is an abbreviation of "descriptor", but why isn't it named as "signature"? Is there any…
akry
  • 615
  • 2
  • 7
  • 14
18
votes
3 answers

When and why might I assign an instance of a descriptor class to a class attribute in Python rather than use a property?

I'm aware that a property is a descriptor, but are there specific examples of when using a descriptor class might be more advantageous, pythonic, or provide some benefit over using @property on a method function?
mkelley33
  • 5,323
  • 10
  • 47
  • 71
18
votes
2 answers

Use cases for property vs. descriptor vs. __getattribute__

The question refers to which one is preferable to be used in which use case, not about the technical background. In python, you can control the access of attributes via a property, a descriptor, or magic methods. Which one is most pythonic in which…
Iodnas
  • 3,414
  • 24
  • 26
17
votes
2 answers

How can I get the attribute name when working with descriptor protocol in Python?

The descriptor protocol works fine but I still have one issue I would like to resolve. I have a descriptor: class Field(object): def __init__(self, type_, name, value=None, required=False): self.type = type_ self.name = "_" +…
Johan Vergeer
  • 5,208
  • 10
  • 48
  • 105
16
votes
3 answers

Why do people default owner parameter to None in __get__?

I've seen this quite often: def __get__(self, instance, owner=None): Why do some people use the default value of None for the the owner parameter? This is even done in the Python docs: descr.__get__(self, obj, type=None) --> value
Markus Meskanen
  • 19,939
  • 18
  • 80
  • 119
15
votes
1 answer

How do function descriptors work?

I was reading a presentation on Pythons' Object model when, in one slide (number 9), the author asserts that Pythons' functions are descriptors. The example he presents to illustrate is similar to this one I wrote: def mul(x, y): return x *…
user6774416
  • 756
  • 5
  • 18
15
votes
4 answers

How to implement __iadd__ for a Python property

I'm trying to create a Python property where in-place adding is handled by a different method than retrieving the value, adding another value and reassigning. So, for a property x on an object o, o.x += 5 should work differently than o.x = o.x +…
ptomato
  • 56,175
  • 13
  • 112
  • 165
1
2 3
32 33