How do I declare an attribute in Python without a value?

Question:

In C# I would go:

string UserName;
string Password;

But now, in Python:

class User:
    UserName
    Password

I receive an error that UserName isn’t defined. Can I not declare a variable without a variable?

Asked By: Sergio Tapia

||

Answers:

First of all, you should rewrite like this:

class User(object):
  def __init__(self, username, password):
    self.username = username
    self.password = password

This way, username and password are instance variables instead of class variables (in your example, they are class variables — all instance variables need to be defined in __init__ as properties of self). Then, you can initialize a User with whatever username and password you want, including None if you truly want them to have no value.

Answered By: danben

In Python, and many other languages, there is a value that means “no value”. In Python, that value is None. So you could do something like this:

class User:
   username = None
   password = None

Those sure sound like instance variables though, and not class variables, so maybe do this:

class User(object):
    def __init__(self):
        self.username = None
        self.password = None

Note how Python assigns the None value implicitly from time to time:

def f():
    pass
g = f() # g now has the value of None
Answered By: Kenan Banks

Just assign the value of None:

self.username = None

Answered By: Lyndsey Ferguson

You can’t. In Python, “Variables” are just names. A name always points (“is bound”) to an object. It’s convention to assign names that don’t yet have a sensible value, but should be present, to None.

You might also be interested in my answer here.

Answered By: balpha

In your code:

class User:
    UserName
    Password
    print(UserName, Password)

UserName and Password and print(UserName, Password) are parsed as expressions. Since they have not been assigned at this point, you get a NameError.

In Python, a variable must be defined with a assignment statement before it can be used in an expression, otherwise you get a NameError. Note that "before" here means "execution order", not "source code order". There’s a bit more to it (import statements, globals, namespace hacks), but let’s keep it simple here.

The idiomatic way to define a variable "without a value" is to assign it the value None.

Also, your code looks like it really wants instance members, and not class members. The idiomatic way to do it, as recognized by some static analysis tools such as pylint, is:

class User(object):
    def __init__(self):
        self.username = None
        self.password = None

Also, it is good Python style to derive all classes from "object", so you use new-style classes, and to name instance variable with the lowercase_with_underscore or initialLowerWithCaps convention. The InitialCaps style is quite universally reserved to class names.

Answered By: ddaa

Dataclasses for python 3.7+:

@dataclass
class User:
    UserName: str
    Password: str
Answered By: validname
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.