#🔒 class methods

46 messages · Page 1 of 1 (latest)

timber osprey
#

can you construct a "protected" method in python?
i.e.:

def _protectedmethod(self, value):
  self.whatever = value
hard boughBOT
#

@timber osprey

Python help channel opened

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.

main lily
#

what does "protected" mean?

stiff goblet
#

they probably mean protected in the Java sense where it is private outside but accessible by subclasses

timber osprey
#

^

#

this

main lily
#

then: no.

timber osprey
#

like i know i can do it with attributes like self._name

stiff goblet
#

There is no real way to do this. what you're doing there can be accessed everywhere, but you might have auto checkers yell at you

main lily
#

Python has nothing like "private" in the Java sense.

timber osprey
#

it does

#

it doesnt work

#

but it does

main lily
timber osprey
#

python uses single underscores for this

stiff goblet
#

you can do self.__private_name but all that does is expand it to self.__ClassName__private_name iirc

timber osprey
#

thats name mangling

#

thats different

#

i dont wanna make it private i wanna make it protected

royal stump
desert garden
#

Python doesn't have the concept of "protected" or even "private" really like other languages have.

#

It's all just conventions.

timber osprey
#

Yeah I’m talking about the conventions

#

I was having trouble naming a method with a trailing underscore so I was wondering if I was doing something wrong if that just wasn’t possible

#

Anyone outside of like standard academic institutions just refuses to engage with this topic at all lol

#

And I get why but it makes it annoying for students forced to learn this

desert garden
#

I'll admit, I'm a bit drunk, so I wouldn't go entirely go by my reluctance to give a definite answer. I just don't want to say something stupid and misleading.

timber osprey
#

😂😂😂

#

I wish I was drunk too man

#

But nah it’s not just you

#

I’ve asked about this before (not specifically about methods)

#

And it’s always the same thing

#

“You can’t do that in Python!”

#

And like, I get it, trust me

#

But I still have to do this assignment so lol

#

Theres a clear disconnect between professional python coders and what’s taught in colleges

#

At least with some things

desert garden
#

For a lot of cases, Python is for prototyping. It lets you write code very quickly without thinking about things like access controls. You aren't constained by the language (which can be a good or a bad thing).

#

If a teacher is trying to map Java concepts to Python, there will be things that won't make a lot of sense.

#

Unfortunately.

timber osprey
#

Yeah and I think a lot of my professors come from Java

#

At the very least not Python I mean they’re all engineers so far

#

Electrical engineers at that

desert garden
#

That is unfortunately pretty typical. Ironically, when I learned Java, my teacher was from a C background and didn't actually know Java. I had to teach myself the basics. Programming has a lot of general ideas, and those ideas don't always map nicely to how different languages work.

Sorry, but I'm going offline. Have a good night.

hard boughBOT
#
Python help channel closed

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.