이 클래스 변수가 초기화되었지만 None 인 이유는 무엇입니까?

Lmiguelvargasf

나는 Kivy를 배우고 있으며이 코드 조각이 위젯을 캐시하는 것이라면 목적을 알고 있지만 무슨 일이 일어나고 있는지 이해하는 데 어려움이 있습니다.

class WeatherRoot(BoxLayout):
    current_weather = ObjectProperty()

    def show_current_weather(self, location=None):
        self.clear_widgets()

        print(WeatherRoot.current_weather)
        print(self.current_weather)

        if location is None and self.current_weather is None:
            location = 'New York (US)'

        if location is not None:
            self.current_weather = Factory.CurrentWeather()
            self.current_weather.location = location
        self.add_widget(self.current_weather)

문제는 current_weather이것이 클래스 변수라는 것을 아는 한으로 정의되어 ObjectProperty있으며이 변수를 재정의하는 인스턴스 변수가 없기 때문에 (내 생각에는 그렇게 생각합니다) self.current_weather참조 할 때 클래스를 참조하고 있습니다 변수, 그래서 나는 그것이 self.current_weather와 동일 하다고 생각 WeatherRoot.current_weather하지만, 그 변수를 인쇄했을 때 둘 다 ObjectProperty인스턴스 가 될 것으로 예상했기 때문에 그렇지 않습니다 .

<ObjectProperty name=current_weather>
None

내 요점은이 변수가 None클래스 변수이기 때문에 결코 될 수 없다는 것입니다. 그래서 항상 ObjectProperty이지만 가능해 보이며 그 None이유를 이해할 수 없습니다.

다음은이 애플리케이션의 GUI입니다.

여기에 이미지 설명 입력

여기에 이미지 설명 입력

이것은 내 Kivy 파일입니다.

WeatherRoot:
<WeatherRoot>:
    AddLocationForm

<LocationButton>:
    on_press: app.root.show_current_weather(self.text)

<AddLocationForm>:
    orientation: 'vertical'
    search_input: search_box
    search_results: search_results_list
    BoxLayout:
        height: '40dp'
        size_hint_y: None
        TextInput:
            id: search_box
            size_hint_x: 50
            focus: True
            multiline: False
            on_text_validate: root.search_location()
        Button:
            text: 'Search'
            size_hint_x: 25
            on_press: root.search_location()
        Button:
            text: 'Current Search'
            size_hint_x: 25
    ListView:
        id: search_results_list
        adapter:
            ListAdapter(data=[], cls=main.LocationButton)
    Button:
        height: '40dp'
        size_hint_y: None
        text: 'Cancel'
        on_press: app.root.show_current_weather(None)

따라서 취소 버튼을 눌렀을 때 이전에 검색된 위치가없는 경우에서 볼 수 있듯이 기본값이 하드 코딩됩니다 'New York (US). 이전에 위치를 검색하고 취소 버튼을 누르면이 위치가 표시됩니다.

누군가이 변수로 무슨 일이 일어나고 있는지 설명해 주 current_weather시겠습니까? 이 클래스 변수가 필요하지 않다고 생각했지만 삭제했을 때 앱이 충돌했습니다.

필요한 경우 이것은 내가 가진 전체 코드입니다.

import json

from kivy.app import App
from kivy.network.urlrequest import UrlRequest
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.listview import ListItemButton
from kivy.factory import Factory

class WeatherRoot(BoxLayout):
    current_weather = ObjectProperty()

    def show_current_weather(self, location=None):
        self.clear_widgets()

        print(WeatherRoot.current_weather)
        print(self.current_weather)

        if location is None and self.current_weather is None:
            location = 'New York (US)'

        if location is not None:
            self.current_weather = Factory.CurrentWeather()
            self.current_weather.location = location
        self.add_widget(self.current_weather)

    def show_add_location_form(self):
        self.clear_widgets()
        self.add_widget(AddLocationForm())


class LocationButton(ListItemButton):
    pass


class AddLocationForm(BoxLayout):
    search_input = ObjectProperty()
    search_results = ObjectProperty()

    def search_location(self):
        search_template = 'http://api.openweathermap.org/' \
                          'data/2.5/find?q={}&type=like&APPID=' \
                          '090428d02304be901047796d430986e3'
        search_url = search_template.format(self.search_input.text)
        print(search_url)
        request = UrlRequest(search_url, self.found_location)

    def found_location(self, request, data):
        data = json.loads(data.decode()) if not isinstance(data, dict) else data
        cities = ['{} ({})'.format(d['name'], d['sys']['country'])
                  for d in data['list']]
        # self.search_results.item_strings = cities
        self.search_results.adapter.data.clear()
        self.search_results.adapter.data.extend(cities)
        self.search_results._trigger_reset_populate()


class WeatherApp(App):
    pass


WeatherApp().run()
험악한

속성은 설명자 입니다. 클래스 수준에서 정의되었지만 인스턴스 수준의 동작이 있으며 첫 번째 근사값은 인스턴스 변수처럼 동작합니다.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

템플릿 클래스의 인라인 정적 변수가 초기화되지 않은 이유는 무엇입니까?

분류에서Dev

내 클래스 변수가 _construct 함수에 의해 초기화되지 않는 이유는 무엇입니까?

분류에서Dev

수퍼 클래스의 정적 블록 / 정적 변수가 메인보다 먼저 초기화되는 이유는 무엇입니까?

분류에서Dev

이 세션에서 시퀀스가 초기화되었는지 확인하는 방법은 무엇입니까?

분류에서Dev

빈 기본 클래스도 멤버 변수 인 경우 빈 기본 최적화가 금지되는 이유는 무엇입니까?

분류에서Dev

내장 클래스의 하위 클래스에 대해 인스턴스 변수가 inspect에 나열되지 않는 이유는 무엇입니까?

분류에서Dev

클래스 변수가 변경되는 이유는 무엇입니까?

분류에서Dev

클래스의 데이터 멤버가 0으로 초기화되는 원인은 무엇입니까?

분류에서Dev

이 클래스 이니셜 라이저에 기본값 인 None을 사용하는 이유는 무엇입니까?

분류에서Dev

중첩 된 Python 클래스 인스턴스가 튜플이되는 이유는 무엇입니까?

분류에서Dev

변수가 파이썬의 특정 클래스인지 확인하는 방법은 무엇입니까?

분류에서Dev

C ++ 클래스 인스턴스가 초기화되지 않았지만 컴파일 오류가없는 이유

분류에서Dev

클래스 인스턴스가 목록에 복사 될 때 클래스 내의 변환 목록이 삭제되는 이유는 무엇입니까?

분류에서Dev

추상 클래스가 봉인되거나 정적 일 수없는 이유는 무엇입니까?

분류에서Dev

ImmutableList <T>가 클래스이지만 ImmutableArray <T>가 구조체 인 이유는 무엇입니까?

분류에서Dev

기본 클래스 포인터가 기본 클래스 변수 값이 아닌 파생 클래스 변수 값만 가져 오는 이유는 무엇입니까?

분류에서Dev

정적 내부 클래스를 인스턴스화 할 수있는 이유는 무엇입니까?

분류에서Dev

Java에서 추상 클래스 Number를 인스턴스화 할 수있는 이유는 무엇입니까?

분류에서Dev

attr_accessor를 사용하여 클래스에 인스턴스 변수가 설정되지 않은 이유는 무엇입니까?

분류에서Dev

C # : 파생 클래스가 기본 클래스의 인터페이스 메서드 구현을 재정의 할 수없는 이유는 무엇입니까?

분류에서Dev

staticmethod가 "staticmethod"클래스에 바인딩되지 않는 이유는 무엇입니까?

분류에서Dev

xs : any가 jaxb 클래스와 자동 바인딩되지 않는 이유는 무엇입니까?

분류에서Dev

이 클래스 메서드의이 인스턴스가이 인수를 사용하지 않는 이유는 무엇입니까?

분류에서Dev

최종 로컬 변수가 그렇지 않은데 최종 인스턴스 변수에 초기화가 필요한 이유는 무엇입니까?

분류에서Dev

내부 클래스는 기본 클래스의 개인 최종 메서드에 액세스 할 수 있지만 그 이유는 무엇입니까?

분류에서Dev

Spring은 내 양식을 제출할 때만 내 클래스를 주입합니다. 변수가 null 인 이유는 무엇입니까?

분류에서Dev

Spring은 내 양식을 제출할 때만 내 클래스를 주입합니다. 변수가 null 인 이유는 무엇입니까?

분류에서Dev

파이썬 데이터 클래스, 초기화 인수의 유효성을 검사하는 파이썬적인 방법은 무엇입니까?

분류에서Dev

이 클래스를 초기화 할 때 목록 초기화가 호출되지 않는 이유는 무엇입니까?

Related 관련 기사

  1. 1

    템플릿 클래스의 인라인 정적 변수가 초기화되지 않은 이유는 무엇입니까?

  2. 2

    내 클래스 변수가 _construct 함수에 의해 초기화되지 않는 이유는 무엇입니까?

  3. 3

    수퍼 클래스의 정적 블록 / 정적 변수가 메인보다 먼저 초기화되는 이유는 무엇입니까?

  4. 4

    이 세션에서 시퀀스가 초기화되었는지 확인하는 방법은 무엇입니까?

  5. 5

    빈 기본 클래스도 멤버 변수 인 경우 빈 기본 최적화가 금지되는 이유는 무엇입니까?

  6. 6

    내장 클래스의 하위 클래스에 대해 인스턴스 변수가 inspect에 나열되지 않는 이유는 무엇입니까?

  7. 7

    클래스 변수가 변경되는 이유는 무엇입니까?

  8. 8

    클래스의 데이터 멤버가 0으로 초기화되는 원인은 무엇입니까?

  9. 9

    이 클래스 이니셜 라이저에 기본값 인 None을 사용하는 이유는 무엇입니까?

  10. 10

    중첩 된 Python 클래스 인스턴스가 튜플이되는 이유는 무엇입니까?

  11. 11

    변수가 파이썬의 특정 클래스인지 확인하는 방법은 무엇입니까?

  12. 12

    C ++ 클래스 인스턴스가 초기화되지 않았지만 컴파일 오류가없는 이유

  13. 13

    클래스 인스턴스가 목록에 복사 될 때 클래스 내의 변환 목록이 삭제되는 이유는 무엇입니까?

  14. 14

    추상 클래스가 봉인되거나 정적 일 수없는 이유는 무엇입니까?

  15. 15

    ImmutableList <T>가 클래스이지만 ImmutableArray <T>가 구조체 인 이유는 무엇입니까?

  16. 16

    기본 클래스 포인터가 기본 클래스 변수 값이 아닌 파생 클래스 변수 값만 가져 오는 이유는 무엇입니까?

  17. 17

    정적 내부 클래스를 인스턴스화 할 수있는 이유는 무엇입니까?

  18. 18

    Java에서 추상 클래스 Number를 인스턴스화 할 수있는 이유는 무엇입니까?

  19. 19

    attr_accessor를 사용하여 클래스에 인스턴스 변수가 설정되지 않은 이유는 무엇입니까?

  20. 20

    C # : 파생 클래스가 기본 클래스의 인터페이스 메서드 구현을 재정의 할 수없는 이유는 무엇입니까?

  21. 21

    staticmethod가 "staticmethod"클래스에 바인딩되지 않는 이유는 무엇입니까?

  22. 22

    xs : any가 jaxb 클래스와 자동 바인딩되지 않는 이유는 무엇입니까?

  23. 23

    이 클래스 메서드의이 인스턴스가이 인수를 사용하지 않는 이유는 무엇입니까?

  24. 24

    최종 로컬 변수가 그렇지 않은데 최종 인스턴스 변수에 초기화가 필요한 이유는 무엇입니까?

  25. 25

    내부 클래스는 기본 클래스의 개인 최종 메서드에 액세스 할 수 있지만 그 이유는 무엇입니까?

  26. 26

    Spring은 내 양식을 제출할 때만 내 클래스를 주입합니다. 변수가 null 인 이유는 무엇입니까?

  27. 27

    Spring은 내 양식을 제출할 때만 내 클래스를 주입합니다. 변수가 null 인 이유는 무엇입니까?

  28. 28

    파이썬 데이터 클래스, 초기화 인수의 유효성을 검사하는 파이썬적인 방법은 무엇입니까?

  29. 29

    이 클래스를 초기화 할 때 목록 초기화가 호출되지 않는 이유는 무엇입니까?

뜨겁다태그

보관