Movatterモバイル変換


[0]ホーム

URL:


Sorry, we no longer support your browser
Please upgrade toMicrosoft Edge,Google Chrome, orFirefox. Learn more about ourbrowser support.
Skip to main content
Stack Overflow
  1. About
  2. For Teams
Loading…
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Return to Answer

title using heading tag
tovmeod
  • 888
  • 6
  • 13

Descriptors

Descriptors

They're the magic behind a whole bunch of core Python features.

When you use dotted access to look up a member (eg, x.y), Python first looks for the member in the instance dictionary. If it's not found, it looks for it in the class dictionary. If it finds it in the class dictionary, and the object implements the descriptor protocol, instead of just returning it, Python executes it. A descriptor is any class that implements the__get__,__set__, or__delete__ methods.

Here's how you'd implement your own (read-only) version of property using descriptors:

class Property(object):    def __init__(self, fget):        self.fget = fget    def __get__(self, obj, type):        if obj is None:            return self        return self.fget(obj)

and you'd use it just like the built-in property():

class MyClass(object):    @Property    def foo(self):        return "Foo!"

Descriptors are used in Python to implement properties, bound methods, static methods, class methods and slots, amongst other things. Understanding them makes it easy to see why a lot of things that previously looked like Python 'quirks' are the way they are.

Raymond Hettinger hasan excellent tutorial that does a much better job of describing them than I do.

Descriptors

They're the magic behind a whole bunch of core Python features.

When you use dotted access to look up a member (eg, x.y), Python first looks for the member in the instance dictionary. If it's not found, it looks for it in the class dictionary. If it finds it in the class dictionary, and the object implements the descriptor protocol, instead of just returning it, Python executes it. A descriptor is any class that implements the__get__,__set__, or__delete__ methods.

Here's how you'd implement your own (read-only) version of property using descriptors:

class Property(object):    def __init__(self, fget):        self.fget = fget    def __get__(self, obj, type):        if obj is None:            return self        return self.fget(obj)

and you'd use it just like the built-in property():

class MyClass(object):    @Property    def foo(self):        return "Foo!"

Descriptors are used in Python to implement properties, bound methods, static methods, class methods and slots, amongst other things. Understanding them makes it easy to see why a lot of things that previously looked like Python 'quirks' are the way they are.

Raymond Hettinger hasan excellent tutorial that does a much better job of describing them than I do.

Descriptors

They're the magic behind a whole bunch of core Python features.

When you use dotted access to look up a member (eg, x.y), Python first looks for the member in the instance dictionary. If it's not found, it looks for it in the class dictionary. If it finds it in the class dictionary, and the object implements the descriptor protocol, instead of just returning it, Python executes it. A descriptor is any class that implements the__get__,__set__, or__delete__ methods.

Here's how you'd implement your own (read-only) version of property using descriptors:

class Property(object):    def __init__(self, fget):        self.fget = fget    def __get__(self, obj, type):        if obj is None:            return self        return self.fget(obj)

and you'd use it just like the built-in property():

class MyClass(object):    @Property    def foo(self):        return "Foo!"

Descriptors are used in Python to implement properties, bound methods, static methods, class methods and slots, amongst other things. Understanding them makes it easy to see why a lot of things that previously looked like Python 'quirks' are the way they are.

Raymond Hettinger hasan excellent tutorial that does a much better job of describing them than I do.

__delete__ instead of __del__

Descriptors

They're the magic behind a whole bunch of core Python features.

When you use dotted access to look up a member (eg, x.y), Python first looks for the member in the instance dictionary. If it's not found, it looks for it in the class dictionary. If it finds it in the class dictionary, and the object implements the descriptor protocol, instead of just returning it, Python executes it. A descriptor is any class that implements the__get__,__set__, or__del____delete__ methods.

Here's how you'd implement your own (read-only) version of property using descriptors:

class Property(object):    def __init__(self, fget):        self.fget = fget    def __get__(self, obj, type):        if obj is None:            return self        return self.fget(obj)

and you'd use it just like the built-in property():

class MyClass(object):    @Property    def foo(self):        return "Foo!"

Descriptors are used in Python to implement properties, bound methods, static methods, class methods and slots, amongst other things. Understanding them makes it easy to see why a lot of things that previously looked like Python 'quirks' are the way they are.

Raymond Hettinger hasan excellent tutorial that does a much better job of describing them than I do.

Descriptors

They're the magic behind a whole bunch of core Python features.

When you use dotted access to look up a member (eg, x.y), Python first looks for the member in the instance dictionary. If it's not found, it looks for it in the class dictionary. If it finds it in the class dictionary, and the object implements the descriptor protocol, instead of just returning it, Python executes it. A descriptor is any class that implements the__get__,__set__, or__del__ methods.

Here's how you'd implement your own (read-only) version of property using descriptors:

class Property(object):    def __init__(self, fget):        self.fget = fget    def __get__(self, obj, type):        if obj is None:            return self        return self.fget(obj)

and you'd use it just like the built-in property():

class MyClass(object):    @Property    def foo(self):        return "Foo!"

Descriptors are used in Python to implement properties, bound methods, static methods, class methods and slots, amongst other things. Understanding them makes it easy to see why a lot of things that previously looked like Python 'quirks' are the way they are.

Raymond Hettinger hasan excellent tutorial that does a much better job of describing them than I do.

Descriptors

They're the magic behind a whole bunch of core Python features.

When you use dotted access to look up a member (eg, x.y), Python first looks for the member in the instance dictionary. If it's not found, it looks for it in the class dictionary. If it finds it in the class dictionary, and the object implements the descriptor protocol, instead of just returning it, Python executes it. A descriptor is any class that implements the__get__,__set__, or__delete__ methods.

Here's how you'd implement your own (read-only) version of property using descriptors:

class Property(object):    def __init__(self, fget):        self.fget = fget    def __get__(self, obj, type):        if obj is None:            return self        return self.fget(obj)

and you'd use it just like the built-in property():

class MyClass(object):    @Property    def foo(self):        return "Foo!"

Descriptors are used in Python to implement properties, bound methods, static methods, class methods and slots, amongst other things. Understanding them makes it easy to see why a lot of things that previously looked like Python 'quirks' are the way they are.

Raymond Hettinger hasan excellent tutorial that does a much better job of describing them than I do.

fixed `__get__` etc. function names
sharat87
  • 7.6k
  • 12
  • 57
  • 81

Descriptors

They're the magic behind a whole bunch of core Python features.

When you use dotted access to look up a member (eg, x.y), Python first looks for the member in the instance dictionary. If it's not found, it looks for it in the class dictionary. If it finds it in the class dictionary, and the object implements the descriptor protocol, instead of just returning it, Python executes it. A descriptor is any class that implements theget__get__,set__set__, ordel__del__ methods.

Here's how you'd implement your own (read-only) version of property using descriptors:

class Property(object):    def __init__(self, fget):        self.fget = fget    def __get__(self, obj, type):        if obj is None:            return self        return self.fget(obj)

and you'd use it just like the built-in property():

class MyClass(object):    @Property    def foo(self):        return "Foo!"

Descriptors are used in Python to implement properties, bound methods, static methods, class methods and slots, amongst other things. Understanding them makes it easy to see why a lot of things that previously looked like Python 'quirks' are the way they are.

Raymond Hettinger hasan excellent tutorial that does a much better job of describing them than I do.

Descriptors

They're the magic behind a whole bunch of core Python features.

When you use dotted access to look up a member (eg, x.y), Python first looks for the member in the instance dictionary. If it's not found, it looks for it in the class dictionary. If it finds it in the class dictionary, and the object implements the descriptor protocol, instead of just returning it, Python executes it. A descriptor is any class that implements theget,set, ordel methods.

Here's how you'd implement your own (read-only) version of property using descriptors:

class Property(object):    def __init__(self, fget):        self.fget = fget    def __get__(self, obj, type):        if obj is None:            return self        return self.fget(obj)

and you'd use it just like the built-in property():

class MyClass(object):    @Property    def foo(self):        return "Foo!"

Descriptors are used in Python to implement properties, bound methods, static methods, class methods and slots, amongst other things. Understanding them makes it easy to see why a lot of things that previously looked like Python 'quirks' are the way they are.

Raymond Hettinger hasan excellent tutorial that does a much better job of describing them than I do.

Descriptors

They're the magic behind a whole bunch of core Python features.

When you use dotted access to look up a member (eg, x.y), Python first looks for the member in the instance dictionary. If it's not found, it looks for it in the class dictionary. If it finds it in the class dictionary, and the object implements the descriptor protocol, instead of just returning it, Python executes it. A descriptor is any class that implements the__get__,__set__, or__del__ methods.

Here's how you'd implement your own (read-only) version of property using descriptors:

class Property(object):    def __init__(self, fget):        self.fget = fget    def __get__(self, obj, type):        if obj is None:            return self        return self.fget(obj)

and you'd use it just like the built-in property():

class MyClass(object):    @Property    def foo(self):        return "Foo!"

Descriptors are used in Python to implement properties, bound methods, static methods, class methods and slots, amongst other things. Understanding them makes it easy to see why a lot of things that previously looked like Python 'quirks' are the way they are.

Raymond Hettinger hasan excellent tutorial that does a much better job of describing them than I do.

It's *Raymond* Hettinger, not Richard!
tzot
  • 96.7k
  • 30
  • 151
  • 211
Loading
Bold title
tghw
  • 25.4k
  • 13
  • 73
  • 97
Loading
edited body
John Millikin
  • 201.9k
  • 41
  • 217
  • 228
Loading
Post Made Community Wiki byCommunityBot
Nick Johnson
  • 101.2k
  • 17
  • 131
  • 198
Loading
lang-py

[8]ページ先頭

©2009-2025 Movatter.jp