Selecting global or object print function
Indeed, NSView
has a
func print(_ sender: AnyObject?)
method to open the Print panel, which is an unfortunate conincidence.
Your myPrint()
wrapper has some limitations, for example
myPrint("b", appendNewline : false)
does not compile. A better implementation would be
func myPrint<T>(o : T, appendNewline nl: Bool = true) {
print(o, appendNewline: nl)
}
But you can simply prepend the module name "Swift" to refer to the global function explicitly:
Swift.print("xxx")
Expose an object's member functions as global functions
Why don't you just use JavaScript's bind
, call
and apply
methods to call the object's member functions when you need to invoke them? Making them global indicates code smell.
Swift 2.0: `print` function generates Argument passed to call that takes no arguments error
However, the error goes away if we use
Swift.print
instead of just
That means that there is a print()
method defined in your class
or in one of its superclasses, so that print()
is resolved as
the method call self.print()
.
By prefixing the module name "Swift" you refer to the global print()
function instead.
Printing all global variables/local variables?
Type info variables
to list "All global and static variable names" (huge list.
Type info locals
to list "Local variables of current stack frame" (names and values), including static variables in that function.
Type info args
to list "Arguments of the current stack frame" (names and values).
Python: Printing Function To Call a Randomly Generated Class Object
You have a fundamental misunderstanding about how to refer to things in Python.
In Python you have variable names:
ace_h = 1
two_h = 2
three_h = 3
You can call these values by name:
print(ace_h) # => 1
You can put the values into another collection:
cards = [ace_h, two_h, three_h]
print(cards) # [1, 2, 3]
You can print a specific element:
print(cards[0])
You can print a random element:
print(random.choice(cards))
But these all refer to the values 1, 2, 3
.
You can actually see that by checking the id
:
print(id(ace_h))
print(id(cards[0]))
it doesn't matter if you're using the ace_h
or cards[0]
method to name the value, it's the same one.
This works the same for everything in Python - they're all objects. You can give them names of any kind, whether that's variable names, putting them in a list, or a dictionary - as long as id(thing)
is the same, you're talking about the exact same object.
ace_h = Cards("Ace", "Hearts", 14)
two_h = Cards("Two", "Hearts", 2)
three_h = Cards("Three", "Hearts", 3)
cards = [ace_h, two_h, three_h]
print(cards)
for card in cards:
print(id(card))
print(id(ace_h))
print(id(two_h))
print(id(three_h))
random_card = random.choice(cards)
print(id(random_card))
print(random_card.face)
print('random card is ace_h? {}'.format(random_card is ace_h))
print('random card is two_h? {}'.format(random_card is two_h))
print('random card is three_h? {}'.format(random_card is three_h))
You'll be a lot better off if you just think of variables as labels that you attach to values. In your case you're creating Card
values and you're sticking the label ace_h
on it. You're also putting that Card
inside a list where you can refer to it by the name cards[0]
. You could give it any number of names:
another_name = ace_h
cool_name = ace_h
king_arthur = ace_h
sir_lancelot = ace_h
minister_of_silly_walks = ace_h
Now every single one of those names refers to the exact same value. You can check it with id
, and you can see that they all have the right properties with print(dir(ace_h))
and print(dir(minister_of_silly_walks)))
. And you can check that they are the same object with ace_h is minister_of_silly_walks
.
Using global variables in a function
You can use a global variable within other functions by declaring it as global
within each function that assigns a value to it:
globvar = 0
def set_globvar_to_one():
global globvar # Needed to modify global copy of globvar
globvar = 1
def print_globvar():
print(globvar) # No need for global declaration to read value of globvar
set_globvar_to_one()
print_globvar() # Prints 1
Since it's unclear whether globvar = 1
is creating a local variable or changing a global variable, Python defaults to creating a local variable, and makes you explicitly choose the other behavior with the global
keyword.
See other answers if you want to share a global variable across modules.
How can I flush the output of the print function?
In Python 3, print
can take an optional flush
argument:
print("Hello, World!", flush=True)
In Python 2, after calling print
, do:
import sys
sys.stdout.flush()
By default, print
prints to sys.stdout
(see the documentation for more about file objects).
Related Topics
How to Set Cell Spacing and Uicollectionview - Uicollectionviewflowlayout Size Ratio
How to Move Enemy Towards a Moving Player
Xcode 11 & Ios13, Using Uikit Can't Change Background Colour of Uiviewcontroller
Multiple Sheet(Ispresented:) Doesn't Work in Swiftui
How to Print Double Quotes Inside ""
What Is the In-Practice Difference Between Generic and Protocol-Typed Function Parameters
Why Can't I Instantiate an Empty Array of a Nested Class
Swift Beta Performance: Sorting Arrays
How to Declare an Array of Weak References in Swift
Dynamically Hiding View in Swiftui
Swift Beta 6 - Confusing Linker Error Message
Property Initializers Run Before 'Self' Is Available
Make a Swift Dictionary Where the Key Is "Type"
Swift Generics Not Preserving Type
Fix Warning "C-Style For Statement Is Deprecated" in Swift 3