The *args
and **kwargs
is a common idiom to allow arbitrary number of arguments to functions as described in the section more on defining functions in the Python documentation.
The *args
will give you all function parameters as a tuple:
def foo(*args):
for a in args:
print(a)
foo(1)
# 1
foo(1,2,3)
# 1
# 2
# 3
The **kwargs
will give you all
keyword arguments except for those corresponding to a formal parameter as a dictionary.
def bar(**kwargs):
for a in kwargs:
print(a, kwargs[a])
bar(name='one', age=27)
# name one
# age 27
Both idioms can be mixed with normal arguments to allow a set of fixed and some variable arguments:
def foo(kind, *args, **kwargs):
pass
It is also possible to use this the other way around:
def foo(a, b, c):
print(a, b, c)
obj = {'b':10, 'c':'lee'}
foo(100,**obj)
# 100 10 lee
Another usage of the *l
idiom is to unpack argument lists when calling a function.
def foo(bar, lee):
print(bar, lee)
l = [1,2]
foo(*l)
# 1 2
In Python 3 it is possible to use *l
on the left side of an assignment (Extended Iterable Unpacking), though it gives a list instead of a tuple in this context:
first, *rest = [1,2,3,4]
first, *l, last = [1,2,3,4]
Also Python 3 adds new semantic (refer PEP 3102):
def func(arg1, arg2, arg3, *, kwarg1, kwarg2):
pass
Such function accepts only 3 positional arguments, and everything after *
can only be passed as keyword arguments.
Note:
- A Python
dict
, semantically used for keyword argument passing, are arbitrarily ordered. However, in Python 3.6, keyword arguments are guaranteed to remember insertion order.
- "The order of elements in
**kwargs
now corresponds to the order in which keyword arguments were passed to the function." - What’s New In Python 3.6
- In fact, all dicts in CPython 3.6 will remember insertion order as an implementation detail, this becomes standard in Python 3.7.
Since 1.8.5 it's possible to seal and freeze the object, so define the above as:
const DaysEnum = Object.freeze({"monday":1, "tuesday":2, "wednesday":3, ...})
or
const DaysEnum = {"monday":1, "tuesday":2, "wednesday":3, ...}
Object.freeze(DaysEnum)
and voila! JS enums.
However, this doesn't prevent you from assigning an undesired value to a variable, which is often the main goal of enums:
let day = DaysEnum.tuesday
day = 298832342 // goes through without any errors
One way to ensure a stronger degree of type safety (with enums or otherwise) is to use a tool like TypeScript or Flow.
Quotes aren't needed but I kept them for consistency.
Best Answer
You can do it either with logical operators or with formula functions.
https://help.salesforce.com/HTViewHelpDoc?id=customize_functions.htm
Here's what I call "programmer's syntax" because it uses the pipes (
||
) and ampersands (&&
); operators are easier to read for developers.You can also use "Excel syntax" where you have
OR()
function that can take any amount of parameters.Try not to use user aliases in business logic. User can change his own alias, rendering your check useless. See if you can base the check on
$Profile
,$UserRole
or maybe create some checkbox field on the user record, make it editable only for SysAdmins...In this particular scenario - there's a way to check at runtime whether user has the right to delete Accounts. Check Using apex:relatedList in a Visualforce page when the user profile denies access to child objects (you'll need the "isDeletable" version).
Edit (re: comments about
!
and!=
)It is mentioned in the first link I've used you know ;)
!=
and sometimes<>
is what programmers use to write "not equal to". It looks similar enough to "slashed equal sign" (≠) but doesn't require remembering some weird code for that character. This convention is pretty much uniform across many programming languages.As far as you're concerned - the syntax of formulas and Visualforce expressions uses them interchangeably:
=
and==
for "equals"!=
and<>
for "not equals` (the later one works in Excel too)So if you write
IF($User.Alias = 'JohnDoe', 'Allow', 'Deny')
you mean that only he will see "Allow".There's also one more thing that might confuse you - negation operator:
!
standing in front of any expression will convert true ↔ false. This can also be achieved by wrapping the expression you want inNOT(some true/false expression here)
.(The exclamation mark that's means the merge field begins is not meant as negation. So
{!some expression}
means just the logical (or numeric, or text) value of that expression. To negate it you either do{!!expression}
or{!NOT(expression)}
.