Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a class attribute with no default value in python

I'm a C# developer learning python.

If I want to define a class with certain attributes, but not provide default values for the attributes, how is that done?

For example (this is my best guess of how to do it):

class Spam
    eggs = None
    cheese = None

Or, is this legal:

class Spam
    eggs
    cheese

Or something else?

like image 874
David Avatar asked Feb 08 '13 12:02

David


1 Answers

You don't. You assign to the attributes later on, or set them to None.

An attribute without a value is not an attribute. Being dynamic, it's perfectly fine to assign the attribute later on without having to declare it in the class.

like image 111
Martijn Pieters Avatar answered Oct 04 '22 17:10

Martijn Pieters