0

Possible Duplicate:
Python variable declaration

i'm new to python, and i wonder how to put a proper "empty" variables into classes.

For example, in C++, I can put variables like:

public class employee{
private:
int ID;
string name;
public:
.....
}

In python, how do I setup the name and the id without giving them values?? Is it something like:

class employee:
__name__
__id__
...

Also, is it possible to set the data type for each variables?

Community
  • 1
  • 1
user1948847
  • 955
  • 1
  • 12
  • 27
  • 1
    What is that you want to achieve? – Abhijit Jan 15 '13 at 18:54
  • 2
    In your title, you refer to ["Class variables"](http://en.wikipedia.org/wiki/Class_variable), but it seems you are actually talking about ["Instance Variables"](http://en.wikipedia.org/wiki/Instance_variable). A class variable has only one value that is shared between all instances of the class, whereas an instance variable (which is what you appear to be talking about in your C code, which I believe is actually C++ code) has one value for each instance of the class that you create. – Mark Hildreth Jan 15 '13 at 18:54
  • @ Mark I didnt know that lol...And yes it should be c++. Thanks for pointing that out – user1948847 Jan 15 '13 at 19:29

2 Answers2

2

It's true that you can't really do that, but here's what you can do:

class FooBar:
    def __init__(self):
        self.whatever = None

Also, no need to declare datatypes in Python. That's the whole point of a dynamic language!

Don't write python with a C++ accent. Write python the way it was designed and you'll be a lot happier.

Chris Pfohl
  • 18,220
  • 9
  • 68
  • 111
  • 2
    Of course Python has datatypes, lots of them. That's the whole point of strongly-typed languages! (Variables don't have types though - only objects do.) – Kos Jan 15 '13 at 18:56
  • Well - there are datatypes - it just happens to be the object that is the type, rather than the name of it... - python is strongly typed, just not statically typed :) – Jon Clements Jan 15 '13 at 18:56
  • 1
    If there are no datatypes, we should really rename the `TypeError` exception ;) – Voo Jan 15 '13 at 18:58
  • I stand correctly corrected :) I'll fix – Chris Pfohl Jan 15 '13 at 19:02
0

You can't.

There's no concept of empty variables in python, they must be initialized to something. And there's no concept of data type either. In python variable names are just symbols pointing to an object(Objects have types though).

In [37]: x=2      #x refers to an integer

In [38]: x="foo"  #now x refers to a string object

The closest thing to empty variable is to use None or Ellipsis(py 3x only)

In [5]: x=None

In [6]: x

In [7]: x=...        # Ellipsis , py 3.x only

In [8]: x
Out[8]: Ellipsis

But again None and Ellipsis are Objects themselves.

Russell Borogove
  • 18,516
  • 4
  • 43
  • 50
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • `None` is how we say "empty" in Python (but it is a real object too). – Kos Jan 15 '13 at 18:54
  • Another alternative is not to worry about missing attributes, and define `__getattr__` on the class, which would return a default value - but since we don't really know what the OP wants... – Jon Clements Jan 15 '13 at 19:07