0

How can I create variables that all the methods in the class will share (fields or global vars)?

Another question: what is the meaning of ";" in python?

Mat
  • 202,337
  • 40
  • 393
  • 406
pol
  • 315
  • 2
  • 3
  • 7

4 Answers4

1

You need to provide more detail to get more meaningful answers.

There are several ways to make a variable available to the methods of a class;

Instance variable: defined in the scope of the current class instance. Commonly created with __init__(), i.e. when the instance is initiated.

class SomeClass(object):
    def __init__(self):
        self.greeting = 'Hello'
    def greet(self, name):
        return self.greeting + name

Class variable: defined in the class definition. Shared between instances

class SomeClass(object):
    greeting = 'Hello'
    def greet(self, name):
        return self.greeting + name

Note that self.greeting references the class variable greeting via the instance. Assigning to self.greeting does not change the class variable but rather overwrites the greeting instance attribute with the new value. A more explicit, perhaps clearer way of accessing the class variable is self.__class__.greeting

Module (global) variable: defined in the top-level, usually the module.

Define the variable outside the class definition and reference it within your class functions.

greeting = 'Hello'

class SomeClass(object):
    def greet(self, name):
        return greeting + name

There are all sorts of reasons why this is not often a good idea. Search Stackoverflow for 'python globals' and you'll find many explanations about why, as well as cases where it might be appropriate.

Rob Cowie
  • 22,259
  • 6
  • 62
  • 56
0

Answering "Another question"

';' (semicolon) is a statement separator. The Python Grammar defines a simple_stmt as

simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE

A trivial example:

>>> a=1; b=2; c=3
>>> a,b,c
(1, 2, 3)
>>> 
gimel
  • 83,368
  • 10
  • 76
  • 104
0

If you define a variable outside of any function or class, it is available to all the functions and classes within that file. For example:

message="Hello, world"
def speak():
    print message

By default, these variables are read-only. You can't modify message from within speak unless you specifically mark that variable as global:

message="Hello, world"
def speak():
    global message
    message = "hello, python!"
    print message

As for your other question, semicolon is a statement separator:

message="Hello, world" ; print message

It is rarely used, as there's rarely ever a compelling reason to put two statements on a single line.

In the above, the global instance of message will be changed after speak() is called.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
-1

Maybe like this.

class A:
    x = 0
    def inc(self):
        self.__class__.x += 1

b = A()
b.inc()  # A.x == 1, b.x == 1
c = A()  
c.inc()  # A.x == 2, c.x == 2

; is logic line wrap for python, it is not recommended, as it make code difficult to read.

lostyzd
  • 4,515
  • 3
  • 19
  • 33
  • waaaay too complicated for such a simple question. – Bryan Oakley Oct 09 '11 at 14:35
  • @BryanOakley So what's your defination of `complicated`? – lostyzd Oct 09 '11 at 14:50
  • @lostyzd. not sure what bryan means by "complicated", but your code is certainly an uncommon way of writing a class method. in more recent versions of python (>=2.4), this would be done with a classmethod decorator. – ekhumoro Oct 09 '11 at 16:33
  • @ekhumoro Thanks, I know howto write a classmethod by `@staticmethod`, I didn't mean to write a class method, this could be used in `__init__`. – lostyzd Oct 09 '11 at 16:33
  • 1
    @lostyzd. `@classmethod` is not the same as `@staticmethod`. with a classmethod, the class is passed as first argument, so you could have written `cls.x += 1`. (with a staticmethod, neither the class nor the instance is passed as first argument). – ekhumoro Oct 09 '11 at 16:40
  • @lostyzd: what's my definition of complicated? In the context of this question and answer, anything that involves the use of advanced features such as `__class__`. This is a newbie trying to learn a very basic concept. – Bryan Oakley Oct 10 '11 at 15:03