문제

사용 get/set 될 것으로 보인에서 일반적으로 자바(다양한 이유로),그러나 나는 거의 볼 Python 사용하는 코드이다.

왜 당신은 당신을 사용하거나 피 get/set 방법에서는 파이썬?

도움이 되었습니까?

해결책

Cool link: Python is not Java :)

In Java, you have to use getters and setters because using public fields gives you no opportunity to go back and change your mind later to using getters and setters. So in Java, you might as well get the chore out of the way up front. In Python, this is silly, because you can start with a normal attribute and change your mind at any time, without affecting any clients of the class. So, don't write getters and setters.

다른 팁

파이썬에서 액세스 할 수 있습 특성을 직접기 때문에 그것은 공용:

class MyClass:

    def __init__(self):
        self.my_attribute = 0  

my_object = MyClass()
my_object.my_attribute = 1 # etc.

당신이 원하는 무언가를 하에 액세스하거나 돌연변이의 특성을 사용할 수 있습니다 :

class MyClass:

    def __init__(self):
        self._my_attribute = 0

    @property
    def my_attribute(self):
        # Do something if you want
        return self._my_attribute

    @my_attribute.setter
    def my_attribute(self, value):
        # Do something if you want
        self._my_attribute = value

결정적으로 클라이언트에 코드를 동일하게 유지됩니다.

Here is what Guido van Rossum says about that in Masterminds of Programming

What do you mean by "fighting the language"?

Guido: That usually means that they're trying to continue their habits that worked well with a different language.

[...] People will turn everything into a class, and turn every access into an accessor method,
where that is really not a wise thing to do in Python; you'll have more verbose code that is
harder to debug and runs a lot slower. You know the expression "You can write FORTRAN in any language?" You can write Java in any language, too.

No, it's unpythonic. The generally accepted way is to use normal data attribute and replace the ones that need more complex get/set logic with properties.

Windows XP SP1 이전 Windows XP SP1보다 오래된 Windows 버전을 지원하지 않으면 getSystemTimes Win32 API 함수 .

성능 카운터 . 을 사용해야합니다.

Your observation is correct. This is not a normal style of Python programming. Attributes are all public, so you just access (get, set, delete) them as you would with attributes of any object that has them (not just classes or instances). It's easy to tell when Java programmers learn Python because their Python code looks like Java using Python syntax!

I definitely agree with all previous posters, especially @Maximiliano's link to Phillip's famous article and @Max's suggestion that anything more complex than the standard way of setting (and getting) class and instance attributes is to use Properties (or Descriptors to generalize even more) to customize the getting and setting of attributes! (This includes being able to add your own customized versions of private, protected, friend, or whatever policy you want if you desire something other than public.)

As an interesting demo, in Core Python Programming (chapter 13, section 13.16), I came up with an example of using descriptors to store attributes to disk instead of in memory!! Yes, it's an odd form of persistent storage, but it does show you an example of what is possible!

Here's another related post that you may find useful as well: Python: multiple properties, one setter/getter

I had come here for that answer(unfortunately i couldn't) . But i found a work around else where . This below code could be alternative for get .
class get_var_lis: def __init__(self): pass def __call__(self): return [2,3,4] def __iter__(self): return iter([2,3,4]) some_other_var = get_var_lis
This is just a workaround . By using the above concept u could easily build get/set methodology in py too.

Our teacher showed one example on class explaining when we should use accessor functions.

class Woman(Human):
    def getAge(self):
        if self.age > 30:
            return super().getAge() - 10
        else:
            return super().getAge()
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top