prototypal
This is an experimental module I created to see if I could get Javascript-like prototypes implemented in Python 3. It's not Pythonic but it works. Feel free to PR if you want to contribute to it.
Usage
from prototypal.prototype import *
# create a new type using @constructor
@constructor
def Person(this, first, last):
this.firstName = first
this.lastName = last
# initialize an instance
bird = Person('Charlie', 'Parker')
print(bird.firstName) # Output: 'Charlie'
print(bird.lastName) # Output: 'Parker'
# dynamically add attributes
bird.instrument = 'alto sax'
print(bird.instrument) # Output: 'alto sax'
# unset attributes just return None
print(bird.age) # Output: None
# add methods to the instance
def sing(this):
print('%s sings!!' % this.lastName)
bird.sing = sing.__get__(bird)
bird.sing() # Output: 'Parker sings!!'
# use the prototype chain to add properties and methods to the type
def getName(this):
return '%s %s' % (this.firstName, this.lastName)
Person.prototype.name = property(getName)
print(bird.name) # Output: 'Charlie Parker'
def greet(this):
print('Hello, my name is %s' % this.name)
Person.prototype.greet = greet.__get__(bird)
bird.greet() # Output: 'Hello, my name is Charlie Parker'
monk = Person('Thelonious', 'Monk')
monk.greet = greet.__get__(monk)
monk.greet() # Output: 'Hello, my name is Thelonious Monk'
# property setter
def setName(this, name):
first, last = name.split(' ')
this.firstName = first
this.lastName = last
Person.prototype.name = property(getName, setName)
bird.name = 'Dizzy Gillespie'
print(bird.firstName) # Output: 'Dizzy'
print(bird.lastName) # Output: 'Gillespie'
# property deleter
def deleteName(this):
print('Deleting %s.' % this.name)
del this.firstName
del this.lastName
Person.prototype.name = property(getName, setName, deleteName)
del bird.name # Output: 'Deleting Dizzy Gillespie.'
print(bird.name) # Output: 'None None'
# using prototype inheritance
father = Person('Tom', 'Bard')
son = Person('Tommy', 'Bard')
son.__proto__ = father
father.eyeColor = 'blue'
print(son.eyeColor) # Output: 'blue'
# prototype chain relationships
assert son.__proto__ == father
assert son.constructor == father.constructor == Person
assert father.__proto__ == Person.prototype
assert Object.prototype.constructor == Object
assert Person.prototype.constructor == Person
assert Person.prototype.__proto__ == Object.prototype
# should work with lists
father.children = [son]
print(len(father.children)) # Output: 1
########################################################
# Usage of the new features
# Creating a prototype with attributes
jane = create_prototype(first='Jane', last='Doe')
# Chaining prototypes
parent_proto = create_prototype(eye_color='blue')
child_proto = chain_prototypes(parent_proto)
# Cloning a prototype
clone_jane = clone_prototype(jane)
# Dynamic attribute setting
jane.middle = 'Ann'
# Representation customization
print(jane) # Output: <Proto object at 0x...>
Metadata
Release files for prototypal 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| prototypal-0.1.1.tar.gz | 5.2 kB | Without provenance | Details for prototypal-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| prototypal-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for prototypal-0.1.1-py3-none-any.whl |
Total release size: 13.0 kB
Release files / prototypal-0.1.1.tar.gz
| Download URL | prototypal-0.1.1.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cbc1bf5100b85a90e73421b09f22aaf84dbfea8a380573554ea0a287afdb90b2
|
|
BLAKE2b-256 checksum How to use checksums |
e16e3e9ed1ddb7c182056827ccde44784f8a49c012bc82fb3775fad3af368bc8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.12.2 CPython/3.11.7
|
Release files / prototypal-0.1.1-py3-none-any.whl
| Download URL | prototypal-0.1.1-py3-none-any.whl |
|---|---|
| Size | 7.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
438519be63a9f95e21a5b4a36a152d700f0dedb3fa217d4e4b99aec6f036f182
|
|
BLAKE2b-256 checksum How to use checksums |
4b297badd4d958150869b3d89865cb0b0f361cd921d62c179f9be2936f39c926
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.12.2 CPython/3.11.7
|
Security for prototypal 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.