How to detect if a variable is null?

Asked

Viewed 7,713 times

4

I’m trying to validate a simple form in Python 3 and I want to make sure it doesn’t record the information if the field nome is empty, but when I make a if to check whether the variable name is empty, it returns to me that it is not null.

Follows the code:

import Imports
import Dados

import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk

Imports.Connect_db()


class Gravar(object):
    def __init__(self):
        # Abre o clipboard
        self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
        self.entry = Gtk.Entry()

        # Abre o xml
        builder = Gtk.Builder()
        builder.add_from_file("form.glade")

        # Pega a janela
        self.window = builder.get_object("window1")

        # Pega a area de texto
        self.text_name = builder.get_object("text_name")
        self.text_passwd = builder.get_object("text_passwd")
        self.text_numb = builder.get_object("text_numb")

        # abre a janela de dialogo
        self.about = builder.get_object("about_dialog")

        # Abre as janelas
        self.window.show()

        builder.connect_signals({"Gtk_main_quit": Gtk.main_quit,
                                 "on_submit_clicked": self.cadastro,
                                 "on_text_name_activate": self.cadastro,
                                 "on_text_passwd_activate": self.cadastro,
                                 "on_text_numb_activate": self.cadastro,
                                 "on_delete_activate": self.delete_text,
                                 "on_about_activate": self.about_window})

    def cadastro(self, widget):
        name = self.text_name.get_text()
        passwd = self.text_passwd.get_text()
        numb = self.text_numb.get_text()

        if name == None:
            print("Nula")
        else:
            print("n nula")

        Dados.commit(name, passwd, numb)

        Gravar.delete_text(self, widget)

    def delete_text(self, widget):
        self.text_name.set_text("")
        self.text_passwd.set_text("")
        self.text_numb.set_text("")

    def about_window(self, widget):
        self.about.run()

        self.about.hide()


if __name__ == "__main__":
    win = Gravar()

    Gtk.main()

Glade

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
  <requires lib="gtk+" version="3.12"/>
  <object class="GtkAboutDialog" id="about_dialog">
    <property name="can_focus">False</property>
    <property name="resizable">False</property>
    <property name="modal">True</property>
    <property name="type_hint">dialog</property>
    <property name="skip_taskbar_hint">True</property>
    <property name="urgency_hint">True</property>
    <property name="gravity">center</property>
    <property name="program_name">Formulario</property>
    <property name="version">2.0</property>
    <property name="comments" translatable="yes">Formulario integrado com python3 e mysql</property>
    <property name="authors">Guilherme</property>
    <property name="artists">Guilherme</property>
    <property name="logo_icon_name">image-missing</property>
    <child internal-child="vbox">
      <object class="GtkBox" id="aboutdialog-vbox1">
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <property name="spacing">2</property>
        <child internal-child="action_area">
          <object class="GtkButtonBox" id="aboutdialog-action_area1">
            <property name="can_focus">False</property>
            <property name="layout_style">end</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">False</property>
            <property name="position">0</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <property name="title" translatable="yes">Cadastro user</property>
    <property name="resizable">False</property>
    <property name="window_position">center</property>
    <property name="gravity">center</property>
    <signal name="destroy" handler="Gtk_main_quit" swapped="no"/>
    <child>
      <object class="GtkBox" id="box1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkMenuBar" id="menubar1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <child>
              <object class="GtkMenuItem" id="menuitem2">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">_Editar</property>
                <property name="use_underline">True</property>
                <child type="submenu">
                  <object class="GtkMenu" id="menu2">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <child>
                      <object class="GtkImageMenuItem" id="delete">
                        <property name="label">gtk-delete</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                        <signal name="activate" handler="on_delete_activate" swapped="no"/>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
            </child>
            <child>
              <object class="GtkMenuItem" id="menuitem4">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">Aj_uda</property>
                <property name="use_underline">True</property>
                <child type="submenu">
                  <object class="GtkMenu" id="menu3">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <child>
                      <object class="GtkImageMenuItem" id="about">
                        <property name="label">gtk-about</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                        <signal name="activate" handler="on_about_activate" swapped="no"/>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkGrid" id="grid2">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="margin_top">36</property>
            <property name="row_spacing">14</property>
            <property name="column_homogeneous">True</property>
            <child>
              <object class="GtkEntry" id="text_name">
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="margin_right">48</property>
                <property name="input_purpose">name</property>
                <signal name="activate" handler="on_text_name_activate" swapped="no"/>
              </object>
              <packing>
                <property name="left_attach">1</property>
                <property name="top_attach">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkEntry" id="text_passwd">
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="margin_right">47</property>
                <property name="visibility">False</property>
                <property name="invisible_char">*</property>
                <property name="input_purpose">password</property>
                <signal name="activate" handler="on_text_passwd_activate" swapped="no"/>
              </object>
              <packing>
                <property name="left_attach">1</property>
                <property name="top_attach">1</property>
              </packing>
            </child>
            <child>
              <object class="GtkEntry" id="text_numb">
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="margin_right">47</property>
                <property name="input_purpose">phone</property>
                <signal name="activate" handler="on_text_numb_activate" swapped="no"/>
              </object>
              <packing>
                <property name="left_attach">1</property>
                <property name="top_attach">2</property>
              </packing>
            </child>
            <child>
              <object class="GtkLabel" id="nome">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">Nome: </property>
                <property name="wrap">True</property>
              </object>
              <packing>
                <property name="left_attach">0</property>
                <property name="top_attach">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkLabel" id="passwd">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">Senha: </property>
              </object>
              <packing>
                <property name="left_attach">0</property>
                <property name="top_attach">1</property>
              </packing>
            </child>
            <child>
              <object class="GtkLabel" id="numb">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">Telefone: </property>
              </object>
              <packing>
                <property name="left_attach">0</property>
                <property name="top_attach">2</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkButtonBox" id="buttonbox1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="orientation">vertical</property>
            <property name="layout_style">start</property>
            <child>
              <object class="GtkButton" id="submit">
                <property name="label" translatable="yes">Gravar</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <property name="margin_top">30</property>
                <property name="margin_bottom">32</property>
                <signal name="clicked" handler="on_submit_clicked" swapped="no"/>
              </object>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">2</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

I leave the name field blank and it returns me the print("n nula").

  • Is something wrong? It seems to me you’re doing it right.

  • So I’m also not understanding why this is happening. I leave the name field blank and it returns me the print("null n")

4 answers

7


It’s working right. You need to decide if you want to know if the variable is null or empty, they’re completely different concepts. The title asks for one thing, the comment asks for another.

A null variable is as if there is no value, nothing.

A variable string empty has a value and its value is empty, which is different from nothing.

Think of a box. If it doesn’t exist it’s null, if it exists and it doesn’t have things inside it’s just empty.

So if you want to know if the variable is empty, and that can only be in specific types like string, check like this:

if name == "":

I put in the Github for future reference.

One string is always delimited in the code by a pair of quotation marks, so if it is empty it only has the quotation marks (which is the box of a string) and nothing in it. What his code did was check the nullity and it was never null, it was existing but empty. The "" it’s like a 0 if it’s a number.

Vazio X Nulo

3

To check whether the variable is null, in addition to the information that was passed above, you can do so too:

if name:
    [...]

0

If it’s string you can do if "string" == "": For objects can be made if objeto == None:

  • 1

    It can make "object is None" even more explicit, because the operator "is" is object identity equality, while == can have some sorcery by the __eq operator implementation__

  • good, did not know this.

  • Vlw for the tip..

0

In python, to know if a variable is null we use is None

if name is None:
 // code

Browser other questions tagged

You are not signed in. Login or sign up in order to post.