Last active
March 6, 2019 12:45
-
-
Save grey-area/9e881539ef60552ffb484de12a067a85 to your computer and use it in GitHub Desktop.
Different methods of creating instances of class with common values for some data attribute
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
# Base class | |
class Base: | |
# 'Constructor' class methods to make instances with common values for data attribute | |
@classmethod | |
def create_common_instance_1(cls): | |
value = np.eye(3) | |
return cls(value) | |
@classmethod | |
def create_common_instance_2(cls): | |
value = np.random.random((4, 4))**0.5 | |
return cls(value) | |
@classmethod | |
def create_common_instance_3(cls): | |
value = np.linalg.inv(np.random.random((3, 3))) | |
return cls(value) | |
def __init__(self, value): | |
self.data = value | |
def some_method(self): | |
pass | |
def some_other_method(self): | |
pass | |
# Creating three common instances | |
v1 = Base.create_common_instance_1() | |
v2 = Base.create_common_instance_2() | |
v3 = Base.create_common_instance_3() | |
# Creating another instance | |
v_other = Base(np.zeros((3, 3))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
# Base class | |
class Base: | |
# Dict for creating instances with common data values | |
init_dict = { | |
'type_1': np.eye(3), | |
'type_2': np.random.random((4, 4))**0.5, | |
'type_3': np.linalg.inv(np.random.random((3, 3))) | |
} | |
# If value is a string, lookup in common values dict, else use value | |
def __init__(self, value): | |
if type(value) is str and value in Base.init_dict.keys(): | |
self.data = Base.init_dict[value] | |
else: | |
self.data = value | |
def some_method(self): | |
pass | |
def some_other_method(self): | |
pass | |
# Creating three common instances | |
v1 = Base('type_1') | |
v2 = Base('type_2') | |
v3 = Base('type_3') | |
# Creating another instance | |
v_other = Base(np.zeros((3, 3))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
# Base class | |
class Base: | |
def __init__(self, value): | |
self.data = value | |
def some_method(self): | |
pass | |
def some_other_method(self): | |
pass | |
# 'Constructor' functions to make instances with common values for data attribute | |
def create_common_instance_1(): | |
value = np.eye(3) | |
return Base(value) | |
def create_common_instance_2(): | |
value = np.random.random((4, 4))**0.5 | |
return Base(value) | |
def create_common_instance_3(): | |
value = np.linalg.inv(np.random.random((3, 3))) | |
return Base(value) | |
# Creating three common instances | |
v1 = create_common_instance_1() | |
v2 = create_common_instance_2() | |
v3 = create_common_instance_3() | |
# Creating another instance | |
v_other = Base(np.zeros((3, 3))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
# Base class | |
class Base: | |
# 'Constructor' static methods to make instances with common values for data attribute | |
@staticmethod | |
def create_common_instance_1(): | |
value = np.eye(3) | |
return Base(value) | |
@staticmethod | |
def create_common_instance_2(): | |
value = np.random.random((4, 4))**0.5 | |
return Base(value) | |
@staticmethod | |
def create_common_instance_3(): | |
value = np.linalg.inv(np.random.random((3, 3))) | |
return Base(value) | |
def __init__(self, value): | |
self.data = value | |
def some_method(self): | |
pass | |
def some_other_method(self): | |
pass | |
# Creating three common instances | |
v1 = Base.create_common_instance_1() | |
v2 = Base.create_common_instance_2() | |
v3 = Base.create_common_instance_3() | |
# Creating another instance | |
v_other = Base(np.zeros((3, 3))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment