Skip to main content

Javascript-like prototypes for Python.

Project description

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...>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

prototypal-0.1.1.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

prototypal-0.1.1-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file prototypal-0.1.1.tar.gz.

File metadata

  • Download URL: prototypal-0.1.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.12.2 CPython/3.11.7

File hashes

Hashes for prototypal-0.1.1.tar.gz
Algorithm Hash digest
SHA256 cbc1bf5100b85a90e73421b09f22aaf84dbfea8a380573554ea0a287afdb90b2
MD5 7d1e083721bcd31a3f4305dc7e34804d
BLAKE2b-256 e16e3e9ed1ddb7c182056827ccde44784f8a49c012bc82fb3775fad3af368bc8

See more details on using hashes here.

File details

Details for the file prototypal-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: prototypal-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.12.2 CPython/3.11.7

File hashes

Hashes for prototypal-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 438519be63a9f95e21a5b4a36a152d700f0dedb3fa217d4e4b99aec6f036f182
MD5 3aa5c554c2b415a12b5f2bfe9e0eda81
BLAKE2b-256 4b297badd4d958150869b3d89865cb0b0f361cd921d62c179f9be2936f39c926

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page