Skip to main content
replaced http://programmers.stackexchange.com/ with https://softwareengineering.stackexchange.com/
Source Link

Given a Python class which will be instantiated only once, i.e. there will be only one object of the class. I was wondering in which cases it makes sense to create a single class instance instead of working directly with the class instead.

There is a similar questionquestion, but it has a different focus:

  1. it is about grouping global variables and functions into a class and
  2. It isn't Python-specific. The latter means it does not consider the fact that (in Python) classes are objects too.

UPDATE:

In Python, I can do the following with both classes and objects:

class A(object):
    pass

A.some_state_var = True
# Then later
A.some_state_var = False


my_a = A()
 
my_a.some_state_var = True
# Then later
my_a.some_state_var = False

So I don't see how the choice between a class and an instance of that class is about state (in Python). I can maintain a state with either of the two.

Furthermore, the motivation to create my class / class instance is not about enforcing a Singleton requirement.

Also, it is not so much about creating a new Type.

The motivation is to group related code and data and having an interface to it. This is why I initially modeled it as a class in a class diagram. Only when it came to the implementation I started wondering whether to instantiate this class or not.

Given a Python class which will be instantiated only once, i.e. there will be only one object of the class. I was wondering in which cases it makes sense to create a single class instance instead of working directly with the class instead.

There is a similar question, but it has a different focus:

  1. it is about grouping global variables and functions into a class and
  2. It isn't Python-specific. The latter means it does not consider the fact that (in Python) classes are objects too.

UPDATE:

In Python, I can do the following with both classes and objects:

class A(object):
    pass

A.some_state_var = True
# Then later
A.some_state_var = False


my_a = A()
 
my_a.some_state_var = True
# Then later
my_a.some_state_var = False

So I don't see how the choice between a class and an instance of that class is about state (in Python). I can maintain a state with either of the two.

Furthermore, the motivation to create my class / class instance is not about enforcing a Singleton requirement.

Also, it is not so much about creating a new Type.

The motivation is to group related code and data and having an interface to it. This is why I initially modeled it as a class in a class diagram. Only when it came to the implementation I started wondering whether to instantiate this class or not.

Given a Python class which will be instantiated only once, i.e. there will be only one object of the class. I was wondering in which cases it makes sense to create a single class instance instead of working directly with the class instead.

There is a similar question, but it has a different focus:

  1. it is about grouping global variables and functions into a class and
  2. It isn't Python-specific. The latter means it does not consider the fact that (in Python) classes are objects too.

UPDATE:

In Python, I can do the following with both classes and objects:

class A(object):
    pass

A.some_state_var = True
# Then later
A.some_state_var = False


my_a = A()
 
my_a.some_state_var = True
# Then later
my_a.some_state_var = False

So I don't see how the choice between a class and an instance of that class is about state (in Python). I can maintain a state with either of the two.

Furthermore, the motivation to create my class / class instance is not about enforcing a Singleton requirement.

Also, it is not so much about creating a new Type.

The motivation is to group related code and data and having an interface to it. This is why I initially modeled it as a class in a class diagram. Only when it came to the implementation I started wondering whether to instantiate this class or not.

added 10 characters in body; deleted 22 characters in body
Source Link
langlauf.io
  • 261
  • 1
  • 2
  • 6

Given a Python class which will be instantiated only once, i.e. there will be only one object of the class. I was wondering in which cases it makes sense to create a single class instance instead of working directly with the class instead.

There is a similar question, but it has a different focus:

  1. it is about grouping global variables and functions into a class and
  2. It isn't Python-specific. The latter means it does not consider the fact that (in Python) classes are objects too.

UPDATE:

In Python, I can do the following with both classes and objects:

class A(object):
    pass

A.some_state_var = True
# Then later
A.some_state_var = False


my_a = A()
 
my_a.some_state_var = True
# Then later
my_a.some_state_var = False

So I don't see how the choice between a class and an instance of that class is about state (in Python). I can maintain a state with either of the two.

Furthermore, the motivation to create my class / class instance is not about enforcing a Singleton requirement.

Also, it is not so much about creating a new Type.

The motivation is to group related code and data and having an interface to it. This is why I initially modeled it as a class in a class diagram. Only when it came to the implementation I started wondering during implementation whether to instantiate this class or not.

Given a Python class which will be instantiated only once, i.e. there will be only one object of the class. I was wondering in which cases it makes sense to create a single class instance instead of working directly with the class instead.

There is a similar question, but it has a different focus:

  1. it is about grouping global variables and functions into a class and
  2. It isn't Python-specific. The latter means it does not consider the fact that (in Python) classes are objects too.

UPDATE:

In Python, I can do the following with both classes and objects:

class A(object):
    pass

A.some_state_var = True
# Then later
A.some_state_var = False


my_a = A()
 
my_a.some_state_var = True
# Then later
my_a.some_state_var = False

So I don't see how the choice between a class and an instance of that class is about state (in Python). I can maintain a state with either of the two.

Furthermore, the motivation to create my class / class instance is not about enforcing a Singleton requirement.

Also, it is not so much about creating a new Type.

The motivation is to group related code and data and having an interface to it. This is why I modeled it as a class in a class diagram. Only when it came to the implementation I started wondering during implementation whether to instantiate this class or not.

Given a Python class which will be instantiated only once, i.e. there will be only one object of the class. I was wondering in which cases it makes sense to create a single class instance instead of working directly with the class instead.

There is a similar question, but it has a different focus:

  1. it is about grouping global variables and functions into a class and
  2. It isn't Python-specific. The latter means it does not consider the fact that (in Python) classes are objects too.

UPDATE:

In Python, I can do the following with both classes and objects:

class A(object):
    pass

A.some_state_var = True
# Then later
A.some_state_var = False


my_a = A()
 
my_a.some_state_var = True
# Then later
my_a.some_state_var = False

So I don't see how the choice between a class and an instance of that class is about state (in Python). I can maintain a state with either of the two.

Furthermore, the motivation to create my class / class instance is not about enforcing a Singleton requirement.

Also, it is not so much about creating a new Type.

The motivation is to group related code and data and having an interface to it. This is why I initially modeled it as a class in a class diagram. Only when it came to the implementation I started wondering whether to instantiate this class or not.

added 4 characters in body; added 13 characters in body
Source Link
langlauf.io
  • 261
  • 1
  • 2
  • 6

Given a Python class which will be instantiated only once, i.e. there will be only one object of the class. I was wondering in which cases it makes sense to create a single class instance instead of working directly with the class instead.

There is a similar question, but it has a different focus:

  1. it is about grouping global variables and functions into a class and
  2. It isn't Python-specific. The latter means it does not consider the fact that (in Python) classes are objects too.

UPDATE:

In Python, I can do the following with both classes and objects:

class A(object):
    pass

A.some_state_var = True
# Then later
A.some_state_var = False


my_a = A()
 
my_a.some_state_var = True
# Then later
my_a.some_state_var = False

So I don't see how the choice between a class and an instance of that class is about state (in Python). I can maintain a state with either of the two.

Furthermore, the motivation to create my class / class instance is not about enforcing a Singleton requirement.

Also, it is not to createso much about creating a new Type.

The motivation is to group related code and data and having an interface to it. This is why I modeled it as a class in a class diagram. Only when it came to the implementation I started wondering during implementation whether to instantiate this class or not.

Given a Python class which will be instantiated only once, i.e. there will be only one object of the class. I was wondering in which cases it makes sense to create a single class instance instead of working directly with the class instead.

There is a similar question, but it has a different focus:

  1. it is about grouping global variables and functions into a class and
  2. It isn't Python-specific. The latter means it does not consider the fact that (in Python) classes are objects too.

UPDATE:

In Python, I can do the following with both classes and objects:

class A(object):
    pass

A.some_state_var = True
# Then later
A.some_state_var = False


my_a = A()
 
my_a.some_state_var = True
# Then later
my_a.some_state_var = False

So I don't see how the choice between a class and an instance of that class is about state (in Python). I can maintain a state with either of the two.

Furthermore, the motivation to create my class / class instance is not about enforcing a Singleton requirement.

Also, it is not to create a new Type.

The motivation is to group related code and data and having an interface to it. This is why I modeled it as a class in a class diagram. Only when it came to implementation I started wondering during implementation whether to instantiate this class or not.

Given a Python class which will be instantiated only once, i.e. there will be only one object of the class. I was wondering in which cases it makes sense to create a single class instance instead of working directly with the class instead.

There is a similar question, but it has a different focus:

  1. it is about grouping global variables and functions into a class and
  2. It isn't Python-specific. The latter means it does not consider the fact that (in Python) classes are objects too.

UPDATE:

In Python, I can do the following with both classes and objects:

class A(object):
    pass

A.some_state_var = True
# Then later
A.some_state_var = False


my_a = A()
 
my_a.some_state_var = True
# Then later
my_a.some_state_var = False

So I don't see how the choice between a class and an instance of that class is about state (in Python). I can maintain a state with either of the two.

Furthermore, the motivation to create my class / class instance is not about enforcing a Singleton requirement.

Also, it is not so much about creating a new Type.

The motivation is to group related code and data and having an interface to it. This is why I modeled it as a class in a class diagram. Only when it came to the implementation I started wondering during implementation whether to instantiate this class or not.

added 852 characters in body
Source Link
langlauf.io
  • 261
  • 1
  • 2
  • 6
Loading
Tweeted twitter.com/#!/StackProgrammer/status/628763872296615938
improved formatting
Source Link
Loading
edited title
Link
langlauf.io
  • 261
  • 1
  • 2
  • 6
Loading
Source Link
langlauf.io
  • 261
  • 1
  • 2
  • 6
Loading