Skip to content

This package is adding private/protected/public fields to Python.

License

Notifications You must be signed in to change notification settings

GulgDev/PyPrivate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 

Repository files navigation

Python private

This package is adding private/protected/public fields to Python.

Without PyPrivate:

class Foo():
	__x = 7 # Private "x"
	def baz(self):
		self.__x += 7

def bar(f, n):
	try:
		getattr(f, n)
	except:
		print('"' + n + '" not found')
	else:
		print('"' + n + '" found')

myfoo = Foo()
myfoo.baz()
bar(myfoo, "__x")     # "__x" not found
bar(myfoo, "_Foo__x") # "_Foo__x" found

With PyPrivate

from privatefields import privatefields

@privatefields
class Foo():
	private_x = 7 # Private "x"
	def baz(self):
		self.private_x += 7

def bar(f, n):
	try:
		getattr(f, n)
	except:
		print('"' + n + '" not found')
	else:
		print('"' + n + '" found')

myfoo = Foo()
myfoo.baz()
bar(myfoo, "private_x") # "private_x" not found
print(dir(myfoo)) # no "x"

How to use

PyPrivate is easy to use. To use private/protected fields import "privatefields":

from privatefields import privatefields

Next, use it with your classes:

@privatefields
class Foo():
	pass

And add prefixes private_/protected_ to your variables/methods names:

self.z           # Public
self.protected_y # Protected
self.private_x   # Private

Friends

You can declare "friend" classes/functions. For this add this line to your class:

class Baz(): # This class can use "x"
	pass
class Bar(): # This class can't use "x"
	pass
@privatefields
class Foo():
	friends = [Baz]
	private_x = "Secret data" # Private "x"

About

This package is adding private/protected/public fields to Python.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages