Understanding Python Properties

Asked 2 years ago, Updated 2 years ago, 25 views

Getting Started I'm a beginner studying Python in a book called Python 3.
I'm currently learning about properties
I'm not sure how the built-in function property() behaves.

See how the property works with the following source code:
I would appreciate it if you could tell me in detail.

class Duck():
    def__init__(self, input_name):
        self.hiden_name=input_name
    default_name(self):
        print('inside the getter')
        return print (self.hiden_name)
    default_name(self, input_name):
        print('inside the setter')
        self.hiden_name=input_name
    name = property(get_name, set_name)

flow = Duck ('Howard')
fowl.name

fowl.name = 'Daffy'
fowl.name

The source code is quoted from this book.
If nothing is substituted for the name, the function of the first argument will be
If something is substituted, the function of the second argument is executed. I thought about it, but I don't understand it.


Other source code using property as a simple example I would appreciate it if you could explain it to me.

python

2022-09-30 20:16

1 Answers

If nothing is substituted for fowl.name, the get_name function will appear. If something is substituted, the input_name argument of the set_name function
You have specified that value

fowl.name is
Same as flow.get_name()
print('inside the getter')
Same as print(flowl.hiden_name)

fowl.name= 'Daffy' is
Same as flow.set_name('Daffy')
print('inside the setter')
Equivalent to flow.hiden_name='Daffy'

Since self.hiddenn_name is named hidden, it is assumed that it will not be accessed from outside.
However, since flow.hiden_name can be accessed from outside,
In this example, the only meaning of setting getter, setter is to print ('inside the getter').
It's easier to understand if self.hiden_name is private as self.__hiden_name.
It's easier to understand if you don't want to access it from the outside by privateizing it and using only the name

The return print (self.hiden_name) is getter, so
You should return the value and return self.hiden_name

getter,setter asks what access you want to grant from the outside
You can "add another action within a function" like print('inside the..')

class Duck():
    def__init__(self, input_name):
        self.__hiden_name=input_name

    def__get_name(self):
        return self.__hiden_name

    def__set_name(self, input_name):
        self.__hiden_name='hello:'+input_name

    name = property(__get_name,__set_name)


flow = Duck ('Howard')
print(fowl.name)
fowl.name = 'Daffy'
print(fowl.name)

# The following is an error.
flow.__get_name()
flow.__set_name('Daffy')
print(flowl.__hiden_name)
flow.__hiden_name='Daffy'


by making non-name private def__get_name(): Delete the __get_name of ... and property and
You can set the value with __set_name from outside, but you cannot retrieve it.
Similarly, if you delete __set_name, you can get the value, but you cannot set it

For name=property(), you can do the same thing with @property, so for more information, try using "decorator getter setter"
By the way, even if you privateize it, you can access it with flow._Duck__hiden_name. I might use it for unittest or something like that


2022-09-30 20:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.