private

Truly Private Variables in Python 3

Truly Private Variables in Python 3 Question: So I know the way to make a variable “private” in python like this: class Foo: def __init__(self): self.__private = ‘bar’ This “works” and doesn’t, as shown below: foo = Foo() ‘__private’ in vars(foo) #False ‘_Foo__private’ in vars(foo) #True Now, I understand this is the way to make …

Total answers: 6

Should I unittest private/protected method

Should I unittest private/protected method Question: This is actually language agnostic. But I’ll give you context in python. I have this parent class class Mammal(object): def __init__(self): “”” do some work “”” def eat(self, food): “””Eat the food””” way_to_eat = self._eating_method() self._consume(food) def _eating_method(self): “””Template method””” def _consume(self, food): “””Template method””” Here eat is the …

Total answers: 3

Inheritance of private and protected methods in Python

Inheritance of private and protected methods in Python Question: I know, there are no ‘real’ private/protected methods in Python. This approach isn’t meant to hide anything; I just want to understand what Python does. class Parent(object): def _protected(self): pass def __private(self): pass class Child(Parent): def foo(self): self._protected() # This works def bar(self): self.__private() # This …

Total answers: 6

When should an attribute be private and made a read-only property?

When should an attribute be private and made a read-only property? Question: I don’t know when an attribute should be private and whether I should use property. I read recently that setters and getters are not pythonic but using the property decorator is OK. But what if I have attribute, that mustn’t be set from …

Total answers: 10

Python OpenSSL generating public and private key pair

Python OpenSSL generating public and private key pair Question: I am having problem finding a command that would generate a public and private key pair using OpenSSL. Could someone show me some example code of this in action. Thank you Asked By: DustBunny || Source Answers: Using the pyOpenSSL bindings: OpenSSL.crypto.PKey().generate_key(type, bits) Generate a public/private …

Total answers: 2

Calling private function within the same class python

Calling private function within the same class python Question: How can i call a private function from some other function within the same class? class Foo: def __bar(arg): #do something def baz(self, arg): #want to call __bar Right now, when i do this: __bar(val) from baz(), i get this: NameError: global name ‘_Foo__createCodeBehind’ is not …

Total answers: 2

Does Python have “private” variables in classes?

Does Python have “private” variables in classes? Question: I’m coming from the Java world and reading Bruce Eckels’ Python 3 Patterns, Recipes and Idioms. While reading about classes, it goes on to say that in Python there is no need to declare instance variables. You just use them in the constructor, and boom, they are …

Total answers: 15

Defining private module functions in python

Defining private module functions in python Question: According to http://www.faqs.org/docs/diveintopython/fileinfo_private.html: Like most languages, Python has the concept of private elements: Private functions, which can’t be called from outside their module However, if I define two files: #a.py __num=1 and: #b.py import a print a.__num when i run b.py it prints out 1 without giving any …

Total answers: 10

Python inheritance – how to disable a function

Python inheritance – how to disable a function Question: In C++ you can disable a function in parent’s class by declaring it as private in the child class. How can this be done in Python? I.E. How can I hide parent’s function from child’s public interface? Asked By: Boris Gorelik || Source Answers: class X(object): …

Total answers: 7