Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Coding style question: What is the recommended way of naming flag class attributes, i.e. attributes being True or False. Styles I can think of are:
class MyClass:
def my_method(self):
self.request = False
class MyClass:
def my_method(self):
self.is_request = False
class MyClass:
def my_method(self):
self.request_flag = False
PEP8 does not seem to give a firm recommendation. Is there a canonical way of doing this?
Considering that booleans are mostly used in coditions, the second way seems most appropriate.
o = MyClass()
...
if o.is_request: # very intuitive
# it's a request
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
This post was edited and submitted for review 23 days ago.
Improve this question
I recently stumbled upon a problem, How do i access a class's instance variables (aka the variables inside __init__) from outside/inside the file without creating an instance of a class (i.e main = main.foo()).
Example:
class foo:
def __init__(self,name):
self.name = name
class bar:
os.mkdir(foo.name)
you can set the variable as global, and then you will be able to access this variable from everywhere. and also modifying it.
Not sure why you are using nested classes, but:
foo_name = None
class main:
class foo:
def __init__(self,name):
global foo_name
foo_name = self.name = name
class bar:
def __init__(self):
print(foo_name)
main.foo("Jonathan")
main.bar()
Prints out "Jonathan"
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Below two variants to initialize a class instance variable. What is the best practice for initializing an instance variable in a class in python and why (maybe none of the suggested variants)?
Assumption: variant a because it might be more explicit?
class Example():
def __init__(self, parameter):
# EITHER
# variant a to initialize var_1
self.var_1 = self.initialize_var_1_variant_a(parameter)
# OR
# variant b to initialize var_1
self.initialize_var_1_variant_b(parameter)
# OR something else
# ...
def initialize_var_1_variant_a(self, parameter):
# complex calculations, var_1 = f(parameter)
result_of_complex_calculations = 123
return result_of_complex_calculations
def initialize_var_1_variant_b(self, parameter):
# complex calculations, var_1 = f(parameter)
result_of_complex_calculations = 123
self.var_1 = result_of_complex_calculations
example_instance = Example("some_parameter")
print(example_instance.var_1)
Variant A is the common way to do this. It is very nice to be able to see all of the class members by looking at __init__, instead of having to dive into the other functions (initialize_var_1_variant_b) to find out exactly what attributes are set.
In general, all member attributes that a class will ever have should be initialized in __init__.
To come at it from another angle, initialize_var_1_variant_a should do as little as possible. Calculating the value of var_1 and saving it as a class attribute are two tasks that can be easily broken apart.
It also opens up the possibility of moving initialize_var_1_variant_a outside of the class itself, so it could be re-used by other parts of your program down the line.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I have many functions that all share the same parameter. They will be inputting and outputting this parameter many times.
For example:
a = foo
a = fun(a)
a = bar(a)
def fun(a):
...
return a
def bar(a):
...
return a
What is more pro-grammatically correct, passing parameters through a function, or having it be globally accessible for all the functions to work with?
a = foo
fun()
bar()
def fun():
global a
...
def bar():
global a
...
The more localised your variables, the better.
This is virtually an axiom for any programming language.
structs in C (and equivalents in other languages such as FORTRAN) grew up from this realisation, and object orientated programming followed shortly after.
For re-usability of method, passing parameter is better way.
I agree with the other answers but just for the sake of completion, as others have pointed out a class sounds like a good idea here. Consider the following.
class myClass(object):
def __init__(self, foo):
self.a = foo
def fun(self):
# do stuff to self.a
def bar(self):
# do something else to self.a
c = myClass(foo)
c.fun()
c.bar()
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I want to create a service class that just has one instance, so should I make that class a singleton, or should I make the methods as classmethods?
class PromoService():
#classmethod
def create_promo(cls, promotion):
#do stuff
return promo
class DiscountPromoService(PromoService):
#classmethod
def create_promo(cls, promo, discount):
promo = super(DiscountPromoService, cls).create_promo(promo)
promo.discount = discount
promo.save()
return promo
The reason I don't want to create it as a module is because I would need to subclass my service. What is the most pythonic way to do this, the above-mentioned way or to make a singleton class?
Short answer: In my opinion it would work.
BUT, In pure pattern's sense, I have been wrestling with this question for a while:
Do python class methods and class attributes essentially behave like a Singleton?
All instances of that class have no bearing on them
Only class itself have access to them
There is always one of them
Yes, pure Singleton Pattern comparison would fail plain and simple but surely its not far off?
Wouldn't call myself a python expert, so happy to know views on this be corrected on my assumptions.
If you want a singleton, go with a singleton. The pattern referenced here works well. You would simply need to do something like:
class PromoService():
__metaclass__ = Singleton
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
So I'm trying to understand the basics of Classes and objects. I have small example that I'm trying to figure out but I am not fully understanding it. Here is the example:
You need to create a simple class such that a call to the meow() function of the class Cat(as in the example below):
kitty = Cat(3)
kitty.meow()
prints "I have 3 lives" to standard output.
This is what I have so far.
class Cat(?):
def __init__(self, kitty):
self.kitty = kitty
def meow(self):
??
kitty = Cat(3) kitty.meow()
class Cat:
def __init__(self, lives):
self.lives = lives
def meow(self):
print "I have " + str(self.lives) + " lives"
kitty = Cat(3)
kitty.meow()