#๐ name mangling
98 messages ยท Page 1 of 1 (latest)
@twilit abyss
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
why are you trying to obfuscate class attributes?
calling them "private"
i guess
and using getters to pull out this info
im not sure entirely
im more so curious about the convention for doing so
should it be __ or _
If you're not sure why you're doing something then you probably don't need to
can we just answer the question lol
Name mangling only happens with double underscore.
im in class my teacher is using _ but previously i was taught __
Single underscore is just nomenclature
single underscores are conventional for private attributes, but no attributes are truly private in python even if you mangle them
thanks this is all i was asking
But generally speaking only beginners think they need to make things private when it's usually not the case
wait so it single or double
also note that implementing getters and setters is usually considered bad practice in python
or are u guys referring to different things?
how come?
because they aren't necessary, there's no purpose
Both mean "private". But single doesn't mangle. Double mangles
whats the difference between it being mangled or just being private
its been explained as a way to call for an attribute i think
Single underscore is just the other telling other users of the class that an attribute is intended to not be modified unless you really know what you're doing
here's how you access an attribute in python:
my_obj.attribute
```simple as
Double underscore mangles it which makes it more difficult to modify
yeah shes saying this is bad idk lol
does this require a seperate step to unmangle it?
they're either talking about a different language or wrong
Well her opinion is contrary to the majority of professional python developers (and the psf)
Um, no
Inside of your class it's still self.__thing
But really nobody uses this in a professional setting
idk if this helps or changes anything
but this is suppose to be in the context of data science?
this is a data structures and algorithms class
Data scientists are notoriously bad programmers
So that just adds evidence that your professor is wrong
this is wrong, in other languages you would make almost everything private, in python this is unfortunately not possible
this is part of some cc class were building using this code she gave us
class CreditCard:
"""A consumer credit card.""" # docstring, the first set of comments after the name of class is considered the help for that class. help(CreditCard)
def __init__(self, customer, bank, acnt, limit): # The constructor is the very first method
"""Create a new credit card instance.
The initial balance is zero.
customer: the name of the customer (e.g., John Bowman )
bank: the name of the bank (e.g., California Savings )
acnt : the account identifier (e.g., 5391 0375 9387 5309 )
limit: credit limit (measured in dollars)
"""
self.__customer = customer
self._bank = bank
self._account = acnt
self._limit = limit
self._balance = 0 # we start with a balance of zero, this is private, nobody can change it
def get_customer(self): # The get functions are a must, they're called accessor functions (methods)
"""Return name of the customer."""
return self._customer
def get_bank(self):
"""Return the bank s name."""
return self._bank
def get_account(self):
"""Return the card identifying number (typically stored as a string)."""
return self._account
def get_limit(self):
"""Return current credit limit."""
return self._limit
def get_balance(self):
"""Return current balance."""
return self._balance
def set_limit(self, limit):
self._limit = limit
We're talking about python here. Not "other languages"
so here ur saying the get_blah stuff is unnecessary?
yep, the set ones are also unnecessary
Yeah. This truly reads like beginner code
what would be the best practice way to handle this then
if i wanted to change the limit of a card here
yes, however, โmaking things privateโ has nothing to do with being a beginner. only a person who is infatuated with python would say that
or if i wanted to call/get the current balance or something
Maybe troll somewhere else
class CreditCard:
"""A consumer credit card.""" # docstring, the first set of comments after the name of class is considered the help for that class. help(CreditCard)
def __init__(self, customer, bank, acnt, limit): # The constructor is the very first method
"""Create a new credit card instance.
The initial balance is zero.
customer: the name of the customer (e.g., John Bowman )
bank: the name of the bank (e.g., California Savings )
acnt : the account identifier (e.g., 5391 0375 9387 5309 )
limit: credit limit (measured in dollars)
"""
self.customer = customer
self.bank = bank
self.account = acnt
self.limit = limit
self.balance = 0 # we start with a balance of zero, this is private, nobody can change it
c = CreditCard(...)
c.bank = whatever
print(c.limit)
maybe do not consider everything which does not match your opinion a troll. i strongly disagree with your opinion and wanted to say that
@olive jackal we're talking about python here, please stop disrupting
Okay well the entirety of the psf disagrees with you
c.limit = 1000.00
I have contributed to the topic, haven't I? it's about private attributes
oh that works lol
this server is about python, these help threads are for help with python, we even said up front that what we're saying is about python
other languages simply aren't relevant
im curious where does this getters/setters thing come from then? like are people just trying to shoehorn this into python from other languages?
yeah, it comes from java which used to be the default college class language
Pretty much. As you can see from our resident trolls people think python should be exactly like <insert their favorite language>
to be clear, if your professor requires you write code like this you should, and then you should stop when you leave their class
it seems the getter method is just overly redundant in a not good way
It even might be the case with your professor...
Often languages used in teaching are a department decision at universities
if i just access these attributes directly
i think writing setter and getter in python is a good practice, even if you can't make the attributes private
to clarify the print(c.limit) will only work in this code because you changed the previous self._limit to self.limit correct?
I want to chime in briefly and say that sometimes it makes sense to use a sort-of implementation of getters/setters in Python. If you're interested in checking that out, check out the @property decorator Python has.
It still keeps the typical Python way of accessing attributes, but also lets you control a bit more of the getter/setter process.
For instance, sometimes I need to do a bit of work between how I store the value internally and what I want to give to the user. A setter/getter makes sense there.
It would also work if you didn't change it and did print(c._limit)
yeah, i removed the leading underscores because now those attributes are meant to be accessed from outside the class
oh lol
sometimes you have an attribute that is meant to be only used internal to the class, which you can indicate with the underscore, but it changes nothing about the behavior
That's where the difference between _ and __ comes in
class foo:
def __init__(self):
self._a = 0
self.__b = 1
bar = foo()
print(bar._a) #works
print(bar.__b) # doesn't work
Now the mangling happens in a predictable way... I never fully remember it without googling it (because again people don't actually do this professionally)
But if you know how it's mangled you can just write that and it works too
But Kat is not wrong. Sometimes it makes sense to have getters and setters. It sort of depends on the environment and if you trust the users of your code
it looks like _CreditCard__customer
Yeah bar._foo__b in my example
Mangling happens with double leading underscore. Did you look at PEP 8? (https://peps.python.org/pep-0008/#method-names-and-instance-variables)
@lunar cobalt did you just ignore this entire thread?
Can you explain this a bit? I tried reading a bit about this decorator but Iโm confused lol
This is the mangled output?
It's a bit of a side quest compared to your current question of name mangling and private vs public attributes in Python. But I thought it was worth bringing up since the getter/setter discussion was brought up. But let me pull together an example that'll explain things
!e
class CreditCard:
def __init__(self, customer, bank, acnt, limit):
self.__customer = customer
self.bank = bank
self.account = acnt
self.limit = limit
self._balance = 10
@property
def balance(self):
return self._balance + 1 # everybody gets an extra one
@balance.setter
def balance(self, new_val):
if new_val > self._balance:
raise ValueError("nice try")
else:
self._balance = new_val
c = CreditCard(1, 2, 3, 4)
print(c.balance)
c.balance = 8
print(c.balance)
c.balance = 100
:x: Your 3.12 eval job has completed with return code 1.
001 | 11
002 | 9
003 | Traceback (most recent call last):
004 | File "/home/main.py", line 25, in <module>
005 | c.balance = 100
006 | ^^^^^^^^^
007 | File "/home/main.py", line 17, in balance
008 | raise ValueError("nice try")
009 | ValueError: nice try
the useful part of getters and setters is when you want to do something beyond simply getting or setting the value
you can do that in python with the property decorator, and still access that attribute as normal, as shown ^
So, the usual getter/setter methodology isn't really a thing in Python. By that I mean something like get_temperature() or set_temperature(). In python we would just do thing.temperature or thing.temperature = 10.
But sometimes we do want a bit more control of what the user is allowed to set and what we give the user when they ask for a value.
For instance, if we're dealing with temperature in units of Kelvin, maybe we want to entirely prevent the user from trying to set the temperature to a negative number (because you can't have negative temperature in Kelvin (for physics folks watching, don't get pedantic with me)).
Also what Scofflaw said
notice that the class keeps an internal _balance, but outside the class it's accessed as balance, which calls the property-defined method, which references the internal _balance
It feels like with this I can the best of both worlds
that's the python design philosophy
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.